python中datetime默认的1990年改为_如何更改numpy datetime64中的年份值?

这种矢量化的解决方案与使用pandas迭代x.replace(year=n)得到的结果相同,但在大型数组上的速度至少快了x10。在

记住datetime64对象被替换为闰年的年份非常重要。使用pythondatetime库时,以下崩溃:datetime(2012,2,29).replace(year=2011)崩溃。在这里,“替换年份”功能只需将2012年2月29日移到2011年3月1日。在

我用的是Numpyv1.13.1。在import numpy as np

import pandas as pd

def replace_year(x, year):

""" Year must be a leap year for this to work """

# Add number of days x is from JAN-01 to year-01-01

x_year = np.datetime64(str(year)+'-01-01') + (x - x.astype('M8[Y]'))

# Due to leap years calculate offset of 1 day for those days in non-leap year

yr_mn = x.astype('M8[Y]') + np.timedelta64(59,'D')

leap_day_offset = (yr_mn.astype('M8[M]') - yr_mn.astype('M8[Y]') - 1).astype(np.int)

# However, due to days in non-leap years prior March-01,

# correct for previous step by removing an extra day

non_leap_yr_beforeMarch1 = (x.astype('M8[D]') - x.astype('M8[Y]')).astype(np.int) < 59

non_leap_yr_beforeMarch1 = np.logical_and(non_leap_yr_beforeMarch1, leap_day_offset).astype(np.int)

day_offset = np.datetime64('1970') - (leap_day_offset - non_leap_yr_beforeMarch1).astype('M8[D]')

# Finally, apply the day offset

x_year = x_year - day_offset

return x_year

x = np.arange('2012-01-01', '2014-01-01', dtype='datetime64[h]')

x_datetime = pd.to_datetime(x)

x_year = replace_year(x, 1992)

x_datetime = x_datetime.map(lambda x: x.replace(year=1992))

print(x)

print(x_year)

print(x_datetime)

print(np.all(x_datetime.values == x_year))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值