numpy和熊猫timedelta错误

怀疑

在Python中,我有一系列使用熊猫生成(或从CSV文件读取)的日期,并且我想为每个日期加上一年。我可以使用熊猫但不能使用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天,有时甚至还有额外的leap秒。(请注意,额外的leap秒,例如2015年6月30日23:59:60发生的那一秒,不能表示为NumPy datetime64s。)

我知道将NumPydatetime64[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, months, days = [f(dates_np) for f in (year, month, day)]
# recompose the datetime64 array after adding 1 to the years
dates3 = combine64(years+1, months, 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, months, days = [f(dates_np) for f in (year, month, day)]
   .....: combine64(years+1, months, days)
   .....: 
100 loops, best of 3: 2.65 ms per loop

当然,pd.tseries.offsets提供了一整套的偏移量,而在使用NumPy datetime64s时,这些偏移量并不容易。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章