pandas 月中的天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28819470/
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-09-13 23:00:05  来源:igfitidea点击:

Numbers of Day in Month

pythonpandas

提问by FooBar

I have a data frame with a date time index, and I would like to multiply some columns with the number of days in that month.

我有一个带有日期时间索引的数据框,我想将一些列与该月的天数相乘。

                   TUFNWGTP  TELFS  t070101  t070102  t070103  t070104  
TUDIARYDATE                                                              
2003-01-03   8155462.672158      2        0        0        0        0   
2003-01-04   1735322.527819      1        0        0        0        0   
2003-01-04   3830527.482672      2       60        0        0        0   
2003-01-02   6622022.995205      4        0        0        0        0   
2003-01-09   3068387.344956      1        0        0        0        0 

Here, I would like to multiply all the columns starting with twith 31. That is, expected output is

在这里,我想将所有t以 31开头的列相乘。也就是说,预期输出是

                   TUFNWGTP  TELFS  t070101  t070102  t070103  t070104  
TUDIARYDATE                                                              
2003-01-03   8155462.672158      2        0        0        0        0   
2003-01-04   1735322.527819      1        0        0        0        0   
2003-01-04   3830527.482672      2     1680        0        0        0   
2003-01-02   6622022.995205      4        0        0        0        0   
2003-01-09   3068387.344956      1        0        0        0        0 

I know that there are some ways using calendaror similar, but given that I'm already using pandas, there must be an easier way - I assume.

我知道有一些使用calendar或类似的方法,但鉴于我已经在使用pandas,必须有一种更简单的方法 - 我假设。

There is no such datetimeproperty, but there is an offsetM- but I don't know how I would use that without massive inefficiency.

没有这样的datetime属性,但有一个偏移量M- 但我不知道我将如何使用它而不会导致效率低下。

回答by Eric Ness

There is now a Series.dt.daysinmonthattribute for datetime series. Here is an example based on Jeff's answer.

现在有一个Series.dt.daysinmonth日期时间系列的属性。这是一个基于杰夫回答的例子。

In [3]: df = pd.DataFrame({'date' : pd.date_range('20120101',periods=15,freq='M') })

In [4]: df['year'] = df['date'].dt.year

In [5]: df['month'] = df['date'].dt.month

In [6]: df['days_in_month'] = df['date'].dt.daysinmonth

In [7]: df
Out[7]:
         date  year  month  days_in_month
0  2012-01-31  2012      1             31
1  2012-02-29  2012      2             29
2  2012-03-31  2012      3             31
3  2012-04-30  2012      4             30
4  2012-05-31  2012      5             31
5  2012-06-30  2012      6             30
6  2012-07-31  2012      7             31
7  2012-08-31  2012      8             31
8  2012-09-30  2012      9             30
9  2012-10-31  2012     10             31
10 2012-11-30  2012     11             30
11 2012-12-31  2012     12             31
12 2013-01-31  2013      1             31
13 2013-02-28  2013      2             28
14 2013-03-31  2013      3             31

回答by Jeff

pd.tslib.monthrangeis an unadvertised / undocumented function that handles the days_in_month calculation (adjusting for leap years). This could/should prob be added as a property to Timestamp/DatetimeIndex.

pd.tslib.monthrange是处理 days_in_month 计算(调整闰年)的未公开/未记录的函数。这可以/应该 prob 作为属性添加到Timestamp/DatetimeIndex.

In [34]: df = DataFrame({'date' : pd.date_range('20120101',periods=15,freq='M') })

In [35]: df['year'] = df['date'].dt.year

In [36]: df['month'] = df['date'].dt.month

In [37]: df['days_in_month'] = df.apply(lambda x: pd.tslib.monthrange(x['year'],x['month'])[1], axis=1)

In [38]: df
Out[38]: 
         date  year  month  days_in_month
0  2012-01-31  2012      1             31
1  2012-02-29  2012      2             29
2  2012-03-31  2012      3             31
3  2012-04-30  2012      4             30
4  2012-05-31  2012      5             31
5  2012-06-30  2012      6             30
6  2012-07-31  2012      7             31
7  2012-08-31  2012      8             31
8  2012-09-30  2012      9             30
9  2012-10-31  2012     10             31
10 2012-11-30  2012     11             30
11 2012-12-31  2012     12             31
12 2013-01-31  2013      1             31
13 2013-02-28  2013      2             28
14 2013-03-31  2013      3             31

回答by su79eu7k

import pandas as pd
from pandas.tseries.offsets import MonthEnd

df['dim'] = (pd.to_datetime(df.index) + MonthEnd(0)).dt.day

You can omit pd.to_datetime(), if your index is already DatetimeIndex.

pd.to_datetime()如果您的索引已经是 ,则可以省略DatetimeIndex

回答by Christian

Here is a little clunky hand-made method to get the number of days in a month

这是一个有点笨拙的手工制作方法来获得一个月的天数

import datetime

def days_in_month(dt):
  next_month = datetime.datetime(
      dt.year + dt.month / 12, dt.month % 12 + 1, 1)
  start_month = datetime.datetime(dt.year, dt.month, 1)
  td = next_month - start_month
  return td.days

For example:

例如:

>>> days_in_month(datetime.datetime.strptime('2013-12-12', '%Y-%m-%d'))
31
>>> days_in_month(datetime.datetime.strptime('2013-02-12', '%Y-%m-%d'))
28
>>> days_in_month(datetime.datetime.strptime('2012-02-12', '%Y-%m-%d'))
29
>>> days_in_month(datetime.datetime.strptime('2012-01-12', '%Y-%m-%d'))
31
>>> days_in_month(datetime.datetime.strptime('2013-11-12', '%Y-%m-%d'))
30

I let you figure out how to read your table and do the multiplication yourself :)

我让你弄清楚如何阅读你的表格并自己做乘法:)