在
Python中,我有一个使用pandas生成(或从CSV文件中读取)的日期数组,我想在每个日期添加一年.我可以使用pandas但不使用numpy.我究竟做错了什么?或者它是熊猫或numpy中的错误?
谢谢!
import numpy as np import pandas as pd from 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') + 1970 def month(dates): "Return an array of the months given an array of datetime64s" return dates.astype('M8[M]').astype('i8') % 12 + 1 def 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') + 1 def 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 parts years,days = [f(dates_np) for f in (year,month,day)] # recompose the datetime64 array after adding 1 to the years dates3 = combine64(years+1,days)
产量
In [185]: dates3 Out[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 loop In [207]: %%timeit .....: years,day)] .....: combine64(years+1,days) .....: 100 loops,best of 3: 2.65 ms per loop
当然,pd.tseries.offsets提供了一整套补偿,在使用NumPy datetime64s时没有简单的副本.