Pandas DateOffset,倒退一天

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

Pandas DateOffset, step back one day

datepandas

提问by tschm

I try to understand why

我试着理解为什么

print(pd.Timestamp("2015-01-01") - pd.DateOffset(day=1))

does not result in

不会导致

pd.Timestamp("2014-12-31")

I am using Pandas 0.18. I run within the CET timezone.

我正在使用 Pandas 0.18。我在 CET 时区运行。

回答by jezrael

You can check pandas.tseries.offsets.DateOffset:

您可以检查pandas.tseries.offsets.DateOffset

*kwds
Temporal parameter that add to or replace the offset value.
Parameters that add to the offset (like Timedelta):

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
  • microseconds
  • nanoseconds

Parameters that replace the offset value:

  • year
  • month
  • day
  • weekday
  • hour
  • minute
  • second
  • microsecond
  • nanosecond

*kwds
添加或替换偏移值的时间参数。
添加到偏移量的参数(如 Timedelta):

  • 小时
  • 分钟
  • 微秒
  • 纳秒

替换偏移值的参数:

  • 工作日
  • 小时
  • 分钟
  • 第二
  • 微秒
  • 纳秒


print(pd.Timestamp("2015-01-01") - pd.DateOffset(days=1))
2014-12-31 00:00:00

Another solution:

另一种解决方案:

print(pd.Timestamp("2015-01-01") - pd.offsets.Day(1))
2014-12-31 00:00:00

Oalso is possible subtract Timedelta:

也可以减去Timedelta

print(pd.Timestamp("2015-01-01") - pd.Timedelta(1, unit='d'))

回答by user881111

pd.DateOffset(day=1)works (ie no error is raised) because "day" is a valid parameter, as is "days".

pd.DateOffset(day=1)有效(即没有出现错误),因为“ day”是一个有效参数,“ days”也是如此。

Look at the below one: "day" resets the actual day, "days" adds to the original day.

看下面的:“ day”重置实际日期,“ days”添加到原来的日期。

pd.Timestamp("2019-12-25") + pd.DateOffset(day=1)

Timestamp('2019-12-0100:00:00')

pd.Timestamp("2019-12-25") + pd.DateOffset(days=1)

Timestamp('2019-12-2600:00:00')

pd.Timestamp( “2019-12- 25”)+ pd.DateOffset(= 1)

时间戳( '2019-12- 0100:00:00')

pd.Timestamp( “2019-12- 25”)+ pd.DateOffset(= 1)

时间戳( '2019-12- 2600:00:00')

回答by sdementen

Day(d) and DateOffset(days=d) do not behave exactly the same when used on timestamps with timezone information (at least on pandas 0.18.0). It looks like DateOffset add 1 day while keeping the hour information while Day adds just 24 hours of elapsed time.

Day(d) 和 DateOffset(days=d) 在带有时区信息的时间戳上使用时的行为并不完全相同(至少在 Pandas 0.18.0 上)。看起来 DateOffset 添加了 1 天,同时保留了小时信息,而 Day 只添加了 24 小时的经过时间。

>>> # 30/10/2016 02:00+02:00 is the hour before the DST change
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.offsets.Day(1))
2016-10-31 01:00:00+01:00
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.DateOffset(days=1))
2016-10-31 02:00:00+01:00