基于一般日期时间格式DD/mm/yyyy创建新列,但遇到错误“ value eRror:bins必须单调增加。”

发布于 2025-02-10 20:26:30 字数 2115 浏览 1 评论 0原文

我有一个带有常规日期格式的熊猫数据框架列,如下所示。我的日期格式为DD/mm/yyyy。

       dates
0   11/04/2017
1   17/04/2017
2   23/04/2017
3   02/04/2017
4   30/03/2017

我想根据此日期列创建一个新列,例如 预期新专栏

   phase 
0   3
1   4
2   5
3   2
4   1

我尝试使用本文中建议的方法的 基于日期列pandas 创建新列

但是我遇到了一个错误,

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [46], in <cell line: 10>()
      1 cutoff = [
      2     '24/04/2017',
      3     '18/04/2017',
   (...)
      6     '31/03/2017',
      7 ]
      9 cutoff = pd.Series(cutoff).astype('datetime64')
---> 10 final_commit['phase'] = pd.cut(final_commit['dates'], cutoff, labels = [ 4, 3, 2, 1])
     11 print(final_commit.sort_values('dates'))

File ~/Library/Python/3.8/lib/python/site-packages/pandas/core/reshape/tile.py:290, in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates, ordered)
    288     # GH 26045: cast to float64 to avoid an overflow
    289     if (np.diff(bins.astype("float64")) < 0).any():
--> 290         raise ValueError("bins must increase monotonically.")
    292 fac, bins = _bins_to_cuts(
    293     x,
    294     bins,
   (...)
    301     ordered=ordered,
    302 )
    304 return _postprocess_for_cut(fac, bins, retbins, dtype, original)

ValueError: bins must increase monotonically.

我为创建新列的截止值如下所示

'24/04/2017' -> phase 5
'18/04/2017' -> phase 4
'12/04/2017' -> phase 3
'06/04/2017' -> phase 2
'31/03/2017' -> phase 1

,我尝试了

cutoff = [
    '24/04/2017',
    '18/04/2017',
    '12/04/2017',
    '06/04/2017',
    '31/03/2017',
]

cutoff = pd.Series(cutoff).astype('datetime64')
final_commit['phase'] = pd.cut(final_commit['dates'], cutoff, labels = [5, 4, 3, 2, 1])
print(final_commit.sort_values('dates'))




任何建议。谢谢。

I have a pandas data frame column with a general date format that looks like the below. My date format is in DD/MM/YYYY.

       dates
0   11/04/2017
1   17/04/2017
2   23/04/2017
3   02/04/2017
4   30/03/2017

I would like to create a new column based on this dates column, e.g.
Expected new column

   phase 
0   3
1   4
2   5
3   2
4   1

I tried to use the method suggested in this post
Create new column based on date column Pandas

But I am encountering an error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [46], in <cell line: 10>()
      1 cutoff = [
      2     '24/04/2017',
      3     '18/04/2017',
   (...)
      6     '31/03/2017',
      7 ]
      9 cutoff = pd.Series(cutoff).astype('datetime64')
---> 10 final_commit['phase'] = pd.cut(final_commit['dates'], cutoff, labels = [ 4, 3, 2, 1])
     11 print(final_commit.sort_values('dates'))

File ~/Library/Python/3.8/lib/python/site-packages/pandas/core/reshape/tile.py:290, in cut(x, bins, right, labels, retbins, precision, include_lowest, duplicates, ordered)
    288     # GH 26045: cast to float64 to avoid an overflow
    289     if (np.diff(bins.astype("float64")) < 0).any():
--> 290         raise ValueError("bins must increase monotonically.")
    292 fac, bins = _bins_to_cuts(
    293     x,
    294     bins,
   (...)
    301     ordered=ordered,
    302 )
    304 return _postprocess_for_cut(fac, bins, retbins, dtype, original)

ValueError: bins must increase monotonically.

My cutoff for creating the new column is as below

'24/04/2017' -> phase 5
'18/04/2017' -> phase 4
'12/04/2017' -> phase 3
'06/04/2017' -> phase 2
'31/03/2017' -> phase 1

Code I tried

cutoff = [
    '24/04/2017',
    '18/04/2017',
    '12/04/2017',
    '06/04/2017',
    '31/03/2017',
]

cutoff = pd.Series(cutoff).astype('datetime64')
final_commit['phase'] = pd.cut(final_commit['dates'], cutoff, labels = [5, 4, 3, 2, 1])
print(final_commit.sort_values('dates'))




Any suggestion is appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-02-17 20:26:30

正如错误所暗示的那样,您需要确保截止单调增加。您可以使用sort_values

cutoff = pd.to_datetime(cutoff, format='%d/%m/%Y').sort_values()
pd.cut(final_commit['dates'], cutoff, labels=[1,2,3,4])

示例

final_commit = pd.DataFrame({
    'dates': pd.to_datetime(['2017-04-15',  '2017-04-03'])
})
pd.cut(final_commit['dates'], cutoff, labels=[1,2,3,4])

#0    3
#1    1
#Name: dates, dtype: category
#Categories (4, int64): [1 < 2 < 3 < 4]

As the error suggests, you need to make sure the cutoff is monotonically increasing. You can pre sort the values using sort_values:

cutoff = pd.to_datetime(cutoff, format='%d/%m/%Y').sort_values()
pd.cut(final_commit['dates'], cutoff, labels=[1,2,3,4])

Example:

final_commit = pd.DataFrame({
    'dates': pd.to_datetime(['2017-04-15',  '2017-04-03'])
})
pd.cut(final_commit['dates'], cutoff, labels=[1,2,3,4])

#0    3
#1    1
#Name: dates, dtype: category
#Categories (4, int64): [1 < 2 < 3 < 4]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文