如何摆脱 MonthEnds 类型

我正在尝试获取 Pandas DataFrame 中开始日期和结束日期之间的月差值。结果并不完全令人满意......


首先,结果是某种格式为 <[value] * MonthEnds> 的 Datetime 类型。我不能用这个来计算。第一个问题是如何将其转换为整数。我尝试了 .n 属性,但随后出现以下错误:


AttributeError: 'Series' object has no attribute 'n'  

二、结局是“失踪”一个月。这可以通过使用其他解决方案/方法来避免吗?或者我应该只在答案上加 1 个月?


为了支持我的问题,我创建了一些简化的代码:


dates = [{'Start':'1-1-2020', 'End':'31-10-2020'}, {'Start':'1-2-2020', 'End':'30-11-2020'}]

df = pd.DataFrame(dates)


df['Start'] = pd.to_datetime(df['Start'], dayfirst=True)

df['End'] = pd.to_datetime(df['End'], dayfirst=True)

df['Duration'] = (df['End'].dt.to_period('M') - df['Start'].dt.to_period('M'))

df

这导致:


    Start       End         Duration

0   2020-01-01  2020-10-31  <9 * MonthEnds>

1   2020-02-01  2020-11-30  <9 * MonthEnds>

首选结果是:


    Start       End         Duration

0   2020-01-01  2020-10-31  10

1   2020-02-01  2020-11-30  10


冉冉说
浏览 153回答 2
2回答

ITMISS

从结束日期中减去开始日期并将时间增量转换为月份。import pandas as pddates = [{'Start':'1-1-2020', 'End':'31-10-2020'}, {'Start':'1-2-2020', 'End':'30-11-2020'}]df = pd.DataFrame(dates)df['Start'] = pd.to_datetime(df['Start'], dayfirst=True)df['End'] = pd.to_datetime(df['End'], dayfirst=True)df['Duration'] = (df['End']-df['Start']).astype('<m8[M]').astype(int)+1print(df)输出:&nbsp; &nbsp; &nbsp; &nbsp;Start&nbsp; &nbsp; &nbsp; &nbsp; End&nbsp; Duration0 2020-01-01 2020-10-31&nbsp; &nbsp; &nbsp; &nbsp; 101 2020-02-01 2020-11-30&nbsp; &nbsp; &nbsp; &nbsp; 10

小怪兽爱吃肉

尝试这个dates = [{'Start':'1-1-2020', 'End':'31-10-2020'}, {'Start':'1-2-2020', 'End':'30-11-2020'}]df = pd.DataFrame(dates)df['Start'] = pd.to_datetime(df['Start'], dayfirst=True)df['End'] = pd.to_datetime(df['End'], dayfirst=True)df['Duration'] = (df['End'] - df['Start']).apply(lambda x:x.days//30)print(df)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python