Python 如何创建一个以年份为频率的 Pandas DatetimeIndex?

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

How to create a pandas DatetimeIndex with year as frequency?

pythonpython-3.xpandasdate-range

提问by imrek

Using the pandas.date_range(startdate, periods=n, freq=f)function you can create a range of pandas Timestampobjects where the freqoptional paramter denotes the frequency (second, minute, hour, day...) in the range.

使用该pandas.date_range(startdate, periods=n, freq=f)函数,您可以创建一系列 pandasTimestamp对象,其中freq可选参数表示该范围内的频率(秒、分钟、小时、天...)。

The documentationdoes not mention the literals that are expected to be passed in, but after a few minutes you can easily find most of them.

文件没有提到,预计要通过的文字,但几分钟后,你可以很容易地找到他们。

  • 's' : second
  • 'min' : minute
  • 'H' : hour
  • 'D' : day
  • 'w' : week
  • 'm' : month
  • 's' : 第二
  • '分钟':分钟
  • 'H' : 小时
  • 'D' : 天
  • 'w' : 周
  • 'm' : 月

However, none of 'y', 'Y', 'yr', etc. create dates with year as frequency. Does anybody know what to pass in, or if it is possible at all?

但是,“y”、“Y”、“yr”等都没有创建以年份为频率的日期。有谁知道要传入什么,或者是否有可能?

采纳答案by jmz

Annual indexing to the beginning or end of the year

年度索引到年初或年底

Frequency is freq='A'for end of year frequency, 'AS'for start of year. Check the aliases in the documentation.

频率是freq='A'年末频率,年初频率'AS'。检查文档中别名

eg. pd.date_range(start=pd.datetime(2000, 1, 1), periods=4, freq='A')

例如。 pd.date_range(start=pd.datetime(2000, 1, 1), periods=4, freq='A')

returns

返回

DatetimeIndex(['2000-12-31', '2001-12-31', '2002-12-31', '2003-12-31'], dtype='datetime64[ns]', freq='A-DEC', tz=None)

DatetimeIndex(['2000-12-31', '2001-12-31', '2002-12-31', '2003-12-31'], dtype='datetime64[ns]', freq='A-DEC', tz=None)

Annual indexing to the beginning of an arbitrary month

任意月份开始的年度索引

If you need it to be annual from a particular time use an anchored offset, eg. pd.date_range(start=pd.datetime(2000, 1, 1), periods=10, freq='AS-AUG')

如果您需要从特定时间开始每年一次,请使用锚定偏移量,例如。pd.date_range(start=pd.datetime(2000, 1, 1), periods=10, freq='AS-AUG')

returns

返回

DatetimeIndex(['2000-08-01', '2001-08-01', '2002-08-01', '2003-08-01'], dtype='datetime64[ns]', freq='AS-AUG', tz=None)

DatetimeIndex(['2000-08-01', '2001-08-01', '2002-08-01', '2003-08-01'], dtype='datetime64[ns]', freq='AS-AUG', tz=None)

Annual indexing from an arbitrary date

从任意日期开始的年度索引

To index from an arbitrary date, begin the series on that date and use a custom DateOffsetobject.

要从任意日期索引,请在该日期开始系列并使用自定义DateOffset对象。

eg. pd.date_range(start=pd.datetime(2000, 9, 10), periods=4, freq=pd.DateOffset(years=1))

例如。 pd.date_range(start=pd.datetime(2000, 9, 10), periods=4, freq=pd.DateOffset(years=1))

returns

返回

DatetimeIndex(['2000-09-10', '2001-09-10', '2002-09-10', '2003-09-10'], dtype='datetime64[ns]', freq='<DateOffset: kwds={'years': 1}>', tz=None)

DatetimeIndex(['2000-09-10', '2001-09-10', '2002-09-10', '2003-09-10'], dtype='datetime64[ns]', freq='<DateOffset: kwds={'years': 1}>', tz=None)

回答by rhozzy

You are able to use multiples for the frequency strings. For example:

您可以对频率字符串使用倍数。例如:

pd.date_range('01/01/2010',periods=10,freq='365D')

pd.date_range('01/01/2010',periods=10,freq='365D')

This code will give you a series with 01/01/2010, 01/01/2011, etc., which I think is what you are looking for. Of course, the issue here is that you will run into problems with leap years.

此代码将为您提供 01/01/2010、01/01/2011 等系列,我认为这正是您要寻找的。当然,这里的问题是你会遇到闰年的问题。

回答by rofls

You can use month and then pick every 12th month:

您可以使用月份,然后每 12 个月选择一次:

months=pandas.date_range(start=datetime.datetime.now(),periods=120,freq='M')
year=[months[11*i] for i in range(12)]

You can also do:

你也可以这样做:

usingDays=pandas.date_range(start=datetime.datetime.now(),periods=10,freq='365D')

but that won't work so well with leap years.

但这在闰年不会奏效。

回答by CT Zhu

With all those hacks, there is a clear way:

有了所有这些黑客,有一个明确的方法:

pd.date_range(start=datetime.datetime.now(),periods=5,freq='A')

A: Annually.

A: 每年。

365D? Really? What about leap years?

365D? 真的吗?什么闰年