Mar*_*end -1 python python-3.x pandas
我最近将我的代码从 Python 3.3 升级到 Python 3.7,它目前抛出一个错误,内容为:
ValueError:不再支持单位“M”和“Y”,因为它们不代表明确的时间增量值持续时间
这令人费解,因为代码在升级前运行良好。
这是代码的违规部分:
df['date_modified'] = (df['date_variable']-pd.to_timedelta(df['years_variable'], unit = 'Y')).dt.date
Run Code Online (Sandbox Code Playgroud)
这是完整的代码:
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
Run Code Online (Sandbox Code Playgroud)
这不是 Python 问题,而是与熊猫有关。
从 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)
Run Code Online (Sandbox Code Playgroud)
输出
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
Run Code Online (Sandbox Code Playgroud)
这是您的代码的较旧修改,可能对您有用
你应该完全忽略它。我只是为了历史目的而保留它。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3926 次 |
| 最近记录: |