python – numpy和pandas timedelta错误

python – numpy和pandas timedelta错误,第1张

概述在 Python中,我有一个使用pandas生成(或从CSV文件中读取)的日期数组,我想在每个日期添加一年.我可以使用pandas但不使用numpy.我究竟做错了什么?或者它是熊猫或numpy中的错误? 谢谢! import numpy as npimport pandas as pdfrom pandas.tseries.offsets import DateOffset# Genera 在 Python中,我有一个使用pandas生成(或从CSV文件中读取)的日期数组,我想在每个日期添加一年.我可以使用pandas但不使用numpy.我究竟做错了什么?或者它是熊猫或numpy中的错误?

谢谢!

import numpy as npimport pandas as pdfrom pandas.tserIEs.offsets import DateOffset# Generate range of dates using pandas.dates = pd.date_range('1980-01-01','2015-01-01')# Add one year using pandas.dates2 = dates + DateOffset(years=1)# Convert result to numpy. THIS WORKS!dates2_np = dates2.values# Convert original dates to numpy array.dates_np = dates.values# Add one year using numpy. THIS FAILS!dates3 = dates_np + np.timedelta64(1,'Y')# TypeError: Cannot get a common Metadata divisor for NumPy datetime Metadata [ns] and [Y] because they have incompatible nonlinear base time units
解决方法 将np.timedelta64(1,’Y’)添加到dtype datetime64 [ns]的数组中不起作用,因为一年不对应于固定的纳秒数.有时一年是365天,有时是366天,有时甚至还有一个额外的闰秒. (注意额外的闰秒,例如2015-06-30 23:59:60发生的闰秒,不能表示为NumPy datetime64s.)

我知道在NumPy datetime64 [ns]数组中添加一年的最简单方法是将其分解为组成部分,例如年,月和日,对整数数组进行计算,然后重构datetime64数组:

def year(dates):    "Return an array of the years given an array of datetime64s"    return dates.astype('M8[Y]').astype('i8') + 1970def month(dates):    "Return an array of the months given an array of datetime64s"    return dates.astype('M8[M]').astype('i8') % 12 + 1def day(dates):    "Return an array of the days of the month given an array of datetime64s"    return (dates - dates.astype('M8[M]')) / np.timedelta64(1,'D') + 1def combine64(years,months=1,days=1,weeks=None,hours=None,minutes=None,seconds=None,milliseconds=None,microseconds=None,nanoseconds=None):    years = np.asarray(years) - 1970    months = np.asarray(months) - 1    days = np.asarray(days) - 1    types = ('<M8[Y]','<m8[M]','<m8[D]','<m8[W]','<m8[h]','<m8[m]','<m8[s]','<m8[ms]','<m8[us]','<m8[ns]')    vals = (years,months,days,weeks,hours,minutes,seconds,milliseconds,microseconds,nanoseconds)    return sum(np.asarray(v,dtype=t) for t,v in zip(types,vals)               if v is not None)# break the datetime64 array into constituent partsyears,days = [f(dates_np) for f in (year,month,day)]# recompose the datetime64 array after adding 1 to the yearsdates3 = combine64(years+1,days)

产量

In [185]: dates3Out[185]: array(['1981-01-01','1981-01-02','1981-01-03',...,'2015-12-30','2015-12-31','2016-01-01'],dtype='datetime64[D]')

尽管看起来代码太多,但它实际上比添加1年的DateOffset更快:

In [206]: %timeit dates + DateOffset(years=1)1 loops,best of 3: 285 ms per loopIn [207]: %%timeit   .....: years,day)]   .....: combine64(years+1,days)   .....: 100 loops,best of 3: 2.65 ms per loop

当然,pd.tseries.offsets提供了一整套补偿,在使用NumPy datetime64s时没有简单的副本.

总结

以上是内存溢出为你收集整理的python – numpy和pandas timedelta错误全部内容,希望文章能够帮你解决python – numpy和pandas timedelta错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1207417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存