Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用1W偏移-希望偏移一周将当前TS推到下周开始 初始问题陈述_Python_Pandas_Timestamp - Fatal编程技术网

Python 使用1W偏移-希望偏移一周将当前TS推到下周开始 初始问题陈述

Python 使用1W偏移-希望偏移一周将当前TS推到下周开始 初始问题陈述,python,pandas,timestamp,Python,Pandas,Timestamp,考虑到时间戳“2020-03-24 10:00”为周二,我希望使用周日期偏移量获得下周开始的周一00:00 我打算了解日期偏移的工作方式 以下是我的尝试,到目前为止都失败了 # ts being timestamp for Tuesday the 24th ts = pd.Timestamp('2020-03-24 10:00') # I am looking for the offset that will give me Monday the 30th 00:00 # Attempt 1

考虑到时间戳“2020-03-24 10:00”为周二,我希望使用周日期偏移量获得下周开始的周一00:00

我打算了解日期偏移的工作方式

以下是我的尝试,到目前为止都失败了

# ts being timestamp for Tuesday the 24th
ts = pd.Timestamp('2020-03-24 10:00')
# I am looking for the offset that will give me Monday the 30th 00:00

# Attempt 1 / by use of to_offset()
off1 = pd.tseries.frequencies.to_offset('1W')
ts1 = ts + off1
# ts1 is set to next Sunday the 29th 00:00, why this specific date?
# Begining of week is Monday the 30th 00:00
ts1
>>> Out: Timestamp('2020-03-29 00:00:00')

# Attempt 2 / by use of DateOffset(weeks=1)
off2 = pd.tseries.offsets.DateOffset(weeks=1)
ts2 = ts + off2
# ts2 is now Tuesday the 31st 00:00
# It is not what I am looking for, but it makes sense.
# This offset is shifting current date to 7 days later, ok.
ts2
>>> Out: Timestamp('2020-03-31 00:00:00')

# Attempt 3 / by use of DateOffset(weekday=1)
off3 = pd.tseries.offsets.DateOffset(weekday=1)
ts3 = ts + off3
# This time, I cannot figure any reason why the timestamp is
# simply not modified.
ts3
>>> Out: Timestamp('2020-03-24 10:00:00')
请问,有人对结果ts1和ts3有任何解释吗。 为了获得它们,在计算之后有什么逻辑

最后,有人知道如何将时间戳值“替换”到下周开始吗?我本想在ts3上获得这个结果,并希望在ts1上获得同样的结果,但目前这是一个失败

已完成的问题陈述 下面给出的第一个答案支持使用锚定的DateOffset,它似乎确实将下周的开始锚定到我的预期:锚定到周一

但是现在,为了保持一致性,如果我使用相同的锚定偏移量来创建一个周期索引,那么周似乎锚定到周二

# ts being timestamp for Tuesday the 24th
ts = pd.Timestamp('2020-03-24 10:00')
ts_end=pd.Timestamp('2020-04-16 10:00')
# Offset
off1 = pd.tseries.frequencies.to_offset('W-MON')
# PeriodIndex
pi = pd.period_range(start=ts_start, end=ts_end, freq=off1)
# Checking anchoring day of created PeriodIndex:
pi[1].start_time
>>> Out: Timestamp('2020-03-31 00:00:00')

<> p> >,请考虑这个代码:

# ts being timestamp for Tuesday the 24th
ts = pd.Timestamp('2020-03-24 10:00')
# use right offset to start with monday
off1 = pd.tseries.frequencies.to_offset('W-MON')
# add values
ts1 = ts + off1
# call normalize to start at midnight
ts1 = ts1.normalize()
有关“使用规范化”的搜索页面,请参见“锚定偏移”

编辑1 回答有关“神秘”编辑的问题:使用pi[0]。开始时间而不是pi[1]。开始时间获取第一个元素。然后你会得到

pi[0].start_time
>>2020-03-24 00:00:00
但是,我无法告诉您为什么它会准确地生成03-24,但很可能该函数会使用频率中指定的工作日查找对应于一周开始的上一个日期,然后使用一周正好7天作为频率。它似乎是由事实烤,如果一个人使用

off1 = pd.tseries.frequencies.to_offset('1W-FRI') #weekday = 4
pi[0].start_time
>> 2020-03-21 00:00:00
pi[0].end_time
>> 2020-03-27 23:59:59.999999999
pi[1].start_time
>> 2020-03-28 00:00:00
pi[1].end_time
>> 2020-04-03 23:59:59.999999999
i、 比赛从星期六开始。您可以使用计算出的所需日期作为如上所示的开始,并将频率指定为“1W”,而不使用锚定

# ts being timestamp for Tuesday the 24th
ts = pd.Timestamp('2020-03-24 10:00')
# use right offset to start with monday
off1 = pd.tseries.frequencies.to_offset('W-MON')
# add values
ts1 = ts + off1
# call normalize to start at midnight
ts1 = ts1.normalize()
#ts1 is 2020-03-30 00:00:00
ts_end=pd.Timestamp('2020-04-16 10:00')
# PeriodIndex
pi = pd.period_range(start=ts1, end=ts_end, freq= to_offset('1W'))

pi[0].start_time
>> 2020-03-30 00:00:00

pi[-1].start_time
>> 2020-04-13 00:00:00

pi[-1].end_time
>> 2020-04-19 23:59:59.999999999

希望有帮助。

来自Python 3.8+的datetime模块有一个新的类方法

假设您有一个datetime对象

from datetime import date
week = ts.week +1
year = ts.year 

new_date = date.fromisocalendar(year,week,1)
print(new_date)

2020-03-30

你好,我想你指的是我要找的方向。我尝试了你的解决方案,虽然它对时间戳和日期偏移量求和有效,但对周期范围无效。我用这个?不一致?完成了问题陈述?。请你进一步评论一下好吗?我做错了什么?非常感谢您的帮助和支持!嗨,终于找到原因了。锚定一周的那一天是它的最后一天。再次感谢您的帮助!你好,谢谢你带来这个。目前,我正在研究熊猫的时间效用,并希望继续朝着这个方向前进。再次感谢。
from datetime import date
week = ts.week +1
year = ts.year 

new_date = date.fromisocalendar(year,week,1)
print(new_date)

2020-03-30