gpt4 book ai didi

python - ValueError : Units 'M' and 'Y' are no longer supported, 因为它们不代表明确的 timedelta 值持续时间

转载 作者:行者123 更新时间:2023-12-05 01:13:37 24 4
gpt4 key购买 nike

我最近将我的代码从 Python 3.3 升级到了 Python 3.7,它目前抛出了一个错误,上面写着:

ValueError:不再支持单位“M”和“Y”,因为它们不代表明确的 timedelta 值持续时间

这令人费解,因为代码在升级之前运行良好。

这是代码中的违规部分:

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"(months) "Y" (year) .

https://pandas-docs.github.io/pandas-docs-travis/whatsnew/v0.25.0.html#other-deprecations

这特别影响 pandas.to_timedelta()pandas.Timedelta()pandas.TimedeltaIndex()

您可以将它们指定为等效的日期。

您必须重写代码以使用天而不是年(和月)。


这里是 the issue on github which led to this deprecation 的链接,这里是 the PR that resolved the issue and effected the deprecation 的链接.


更新:这是您的代码的最新修改

与后面的代码块相比,这对原始代码的更改更少:

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)

关于python - ValueError : Units 'M' and 'Y' are no longer supported, 因为它们不代表明确的 timedelta 值持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60122391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com