Python 查找月末 Pandas DataFrame 系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37354105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:17:03  来源:igfitidea点击:

Find the end of the month Pandas DataFrame Series

pythondatedatetimepandas

提问by Lisle

I have a series within a DataFrame that I read in initially as an object, and then need to convert it to a date in the form of yyyy-mm-dd where dd is the end of the month.

我在 DataFrame 中有一个系列,我最初将其作为对象读入,然后需要将其转换为 yyyy-mm-dd 形式的日期,其中 dd 是月末​​。

As an example, I have DataFrame df with a column Date as an object:

例如,我将 DataFrame df 列 Date 作为对象:

...      Date    ...
...     200104   ...
...     200508   ...

What I want when this is all said and done is a date object:

当这一切都说完后,我想要的是一个日期对象:

...      Date    ...
...  2001-04-30  ...
...  2005-08-31  ...

such that df['Date'].item() returns

这样 df['Date'].item() 返回

datetime.date(2001, 04, 30)

I've used the following code to get almost there, but all my dates are at the beginning of the month, not the end. Please advise.

我已经使用以下代码几乎到达那里,但我所有的日期都在月初,而不是月底。请指教。

df['Date'] = pd.to_datetime(df['Date'], format="%Y%m").dt.date

Note: I've already imported Pandas ad pd, and datetime as dt

注意:我已经将 Pandas ad pd 和 datetime 导入为 dt

回答by root

You can use pandas.tseries.offsets.MonthEnd:

您可以使用pandas.tseries.offsets.MonthEnd

from pandas.tseries.offsets import MonthEnd

df['Date'] = pd.to_datetime(df['Date'], format="%Y%m") + MonthEnd(1)

The 1in MonthEndjust specifies to move one step forward to the next date that's a month end. (Using 0or leaving it blank would also work in your case). If you wanted the last day of the next month, you'd use MonthEnd(2), etc. This should work for any month, so you don't need to know the number days in the month, or anything like that. More offset information can be found in the documentation.

1MonthEnd刚刚指定向前移动一步,这是一个月末的下一个日期。(使用0或留空也适用于您的情况)。如果你想要下个月的最后一天,你会使用MonthEnd(2)等。这应该适用于任何一个月,所以你不需要知道当月的天数或类似的东西。更多偏移信息可以在文档中找到。

Example usage and output:

示例用法和输出:

df = pd.DataFrame({'Date': [200104, 200508, 201002, 201602, 199912, 200611]})
df['EndOfMonth'] = pd.to_datetime(df['Date'], format="%Y%m") + MonthEnd(1)

     Date EndOfMonth
0  200104 2001-04-30
1  200508 2005-08-31
2  201002 2010-02-28
3  201602 2016-02-29
4  199912 1999-12-31
5  200611 2006-11-30

回答by Martien Lubberink

Agreed that root offers is the right method. However, readers who blindly use MonthEnd(1)are in for a surprise if they use the last date of the month as an input:

同意 root 提供的是正确的方法。但是,MonthEnd(1)如果使用本月的最后一天作为输入,盲目使用的读者 会感到惊讶:

In [4]: pd.Timestamp('2014-01-01')+MonthEnd(1)
Out[4]: Timestamp('2014-01-31 00:00:00')

In [5]: pd.Timestamp('2014-01-31')+MonthEnd(1)
Out[5]: Timestamp('2014-02-28 00:00:00')

Using MonthEnd(0)instead gives this:

使用MonthEnd(0)代替给出了这个:

In [7]: pd.Timestamp('2014-01-01')+MonthEnd(0)
Out[7]: Timestamp('2014-01-31 00:00:00')

In [8]: pd.Timestamp('2014-01-31')+MonthEnd(0)
Out[8]: Timestamp('2014-01-31 00:00:00')

#Additional Example
from pandas.tseries.offsets import MonthEnd
# Month End of Current Time's Month in String Format
(pd.Timestamp.now()+MonthEnd(0)).strftime('%Y-%m-%dT00:00:00')

回答by piRSquared

use dateutil.relativedelta. Then add a relative delta to the first of the month.

使用dateutil.relativedelta. 然后将相对增量添加到该月的第一天。

import dateutil.relativedelta as rd

datetime.date(2001, 4, 1) + rd.relativedelta(day=31)

gets you:

让你:

datetime.date(2001, 4, 30)