我最近将我的代码从Python 3.3升级到Python 3.7,它目前抛出一个错误,上面写着:
值错误:不再支持单位"M"和"Y",因为它们不表示明确的时间增量值持续时间
这令人费解,因为代码在升级之前工作正常。
这是代码的违规部分:
df['date_modified'] = (df['date_variable']-pd.to_timedelta(df['years_variable'], unit = 'Y')).dt.date
这是完整的代码:
import pandas as pd
import numpy as np
idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Apr-18', 'Apr-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'A', 'B', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
data = [{'years_variable': 1}, {'years_variable': 5}, {'years_variable': 3}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': 3},{'years_variable': 1}, {'years_variable': 6}, {'years_variable': 3}, {'years_variable': 5}, {'years_variable': 2}, {'years_variable': 3},{'years_variable': 1}, {'years_variable': 9}, {'years_variable': 3}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': 3}, {'years_variable': 6}, {'years_variable': 8}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': 9}]
df = pd.DataFrame(data, index=idx, columns=['years_variable'])
df.index.names=['date_variable','type']
df=df.reset_index()
df['date_variable'] = pd.to_datetime(df['date_variable'],format = '%b-%y') # http://strftime.org/
df=df.set_index(['date_variable','type'])
df=df.reset_index()
print(df)
df['date_modified'] = (df['date_variable']-pd.to_timedelta(df['years_variable'], unit = 'Y')).dt.date
这不是Python的问题,而是与pandas有关的问题。
从版本 0.25.0 开始,库 pandas 放弃了对在 timedelta 函数中使用单位"M"
(月("Y"
(年(的支持。
https://pandas-docs.github.io/pandas-docs-travis/whatsnew/v0.25.0.html#other-deprecations
这特别影响pandas.to_timedelta()
、pandas.Timedelta()
和pandas.TimedeltaIndex()
。
您可以改为使用等效的天数来指定这些天数。
您必须重写代码以使用天数而不是年(和月(。
这是指向导致此弃用的 github 上问题的链接,这是指向解决问题并实现弃用的 PR 的链接。
更新:这是对代码的最新修改
这样对原始代码的更改比对原始代码之后的代码块的更改更少:
import pandas as pd
import numpy as np
idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Apr-18', 'Apr-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'A', 'B', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
data = [{'years_variable': 1}, {'years_variable': 5}, {'years_variable': 3}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': 3},{'years_variable': 1}, {'years_variable': 6}, {'years_variable': 3}, {'years_variable': 5}, {'years_variable': 2}, {'years_variable': 3},{'years_variable': 1}, {'years_variable': 9}, {'years_variable': 3}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': 3}, {'years_variable': 6}, {'years_variable': 8}, {'years_variable': 2}, {'years_variable': 7}, {'years_variable': 9}]
df = pd.DataFrame(data, index=idx, columns=['years_variable'])
df.index.names=['date_variable','type']
df=df.reset_index()
df['date_variable'] = pd.to_datetime(df['date_variable'],format = '%b-%y') # http://strftime.org/
df=df.set_index(['date_variable','type'])
df=df.reset_index()
# this is all we're touching
# multiply the values under the 'years_variable' column by 365
# to get the number of days
# and use the 'D' unit in the timedelta, to indicate that it's actually in days
df['date_modified'] = (df['date_variable']-pd.to_timedelta(df['years_variable']*365, unit = 'D')).dt.date
print(df)
输出
date_variable type years_variable date_modified
0 2018-01-01 A 1 2017-01-01
1 2018-01-01 B 5 2013-01-02
2 2018-02-01 B 3 2015-02-02
3 2018-03-01 A 2 2016-03-01
4 2018-03-01 B 7 2011-03-03
5 2018-03-01 C 3 2015-03-02
6 2018-04-01 A 1 2017-04-01
7 2018-04-01 B 6 2012-04-02
8 2018-05-01 B 3 2015-05-02
9 2018-06-01 A 5 2013-06-02
10 2018-06-01 B 2 2016-06-01
11 2018-06-01 C 3 2015-06-02
12 2018-07-01 A 1 2017-07-01
13 2018-08-01 B 9 2009-08-03
14 2018-08-01 C 3 2015-08-02
15 2018-09-01 A 2 2016-09-01
16 2018-09-01 B 7 2011-09-03
17 2018-10-01 C 3 2015-10-02
18 2018-10-01 A 6 2012-10-02
19 2018-10-01 B 8 2010-10-03
20 2018-11-01 A 2 2016-11-01
21 2018-12-01 B 7 2011-12-03
22 2018-12-01 C 9 2009-12-03
这是代码的较旧修改,可能适合您
你应该完全忽略它。我只是出于历史目的保留它。
import pandas as pd
import numpy as np
idx = [np.array(['Jan-18', 'Jan-18', 'Feb-18', 'Mar-18', 'Mar-18', 'Mar-18','Apr-18', 'Apr-18', 'May-18', 'Jun-18', 'Jun-18', 'Jun-18','Jul-18', 'Aug-18', 'Aug-18', 'Sep-18', 'Sep-18', 'Oct-18','Oct-18', 'Oct-18', 'Nov-18', 'Dec-18', 'Dec-18',]),np.array(['A', 'B', 'B', 'A', 'B', 'C', 'A', 'B', 'B', 'A', 'B', 'C','A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'A', 'B', 'C'])]
# convert the values of the inner dicts from years to days
# because we'll be specifying 'D' units in the `timedelta` function
# as opposed to the now deprecated 'Y' units which we used previously
data = [{'years_variable': 365}, {'years_variable': 1825}, {'years_variable': 1095}, {'years_variable': 730}, {'years_variable': 2555}, {'years_variable': 1095}, {'years_variable': 365}, {'years_variable': 2190}, {'years_variable': 1095}, {'years_variable': 1825}, {'years_variable': 730}, {'years_variable': 1095}, {'years_variable': 365}, {'years_variable': 3285}, {'years_variable': 1095}, {'years_variable': 730}, {'years_variable': 2555}, {'years_variable': 1095}, {'years_variable': 2190}, {'years_variable': 2920}, {'years_variable': 730}, {'years_variable': 2555}, {'years_variable': 3285}]
df = pd.DataFrame(data, index=idx, columns=['years_variable'])
df.index.names=['date_variable','type']
df=df.reset_index()
df['date_variable'] = pd.to_datetime(df['date_variable'],format = '%b-%y') # http://strftime.org/
df=df.set_index(['date_variable','type'])
df=df.reset_index()
# specify 'D' units in the timedelta function
df['date_modified'] = (df['date_variable']-pd.to_timedelta(df['years_variable'], unit='D')).dt.date
print(df)