Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x Pandas可识别当前日期,并过滤与今天相关的日期列';日期_Python 3.x_Pandas - Fatal编程技术网

Python 3.x Pandas可识别当前日期,并过滤与今天相关的日期列';日期

Python 3.x Pandas可识别当前日期,并过滤与今天相关的日期列';日期,python-3.x,pandas,Python 3.x,Pandas,在pandas/python中翻译下面的逻辑有很多困难,所以我甚至没有示例代码或df来使用:x 我运行每日报告,基本上过滤从周一到“今天”的前一天的数据。我有一个日期列[以dt.strftime('%#m/%#d/%Y')格式]。它永远不会超过周一周日的范围 1) 确认运行报告时的“今天”,并确认前一天是哪一天。过滤今天日期之前的星期一的“日期”列[以dt.strftime('%#m/%#d/%Y')格式] 2) 过滤df后,取上面逻辑中有日期的这组行,让它在新列“Date2”中检查日期。如果任

在pandas/python中翻译下面的逻辑有很多困难,所以我甚至没有示例代码或df来使用:x

我运行每日报告,基本上过滤从周一到“今天”的前一天的数据。我有一个日期列[以dt.strftime('%#m/%#d/%Y')格式]。它永远不会超过周一周日的范围

1) 确认运行报告时的“今天”,并确认前一天是哪一天。过滤今天日期之前的星期一的“日期”列[以dt.strftime('%#m/%#d/%Y')格式]

2) 过滤df后,取上面逻辑中有日期的这组行,让它在新列“Date2”中检查日期。如果任何日期在星期一日期之前,则在Date2中,将“Date2”中的所有较早日期更改为“Date”列中的星期一日期

3) 如果“今天”是星期一,则在“日期”列中过滤前一个星期一到-星期天的范围。在进行过滤的同时,执行上述步骤[step 2],但对于“date 2”列中周六和周日日期的任何日期,也将其更改为周五日期

这有意义吗?

以下是步骤:

from datetime import datetime

today = pd.to_datetime(datetime.now().date())

day_of_week = today.dayofweek

last_monday = today - pd.to_timedelta(day_of_week, unit='d') 

# if today is Monday, we need to step back another week
if day_of_week == 0:
    last_monday -= pd.to_timedelta(7, unit='d')

# filter for last Monday
last_monday_flags = (df.Date == last_mon)

# filter for Date2 < last Monday
date2_flags = (df.Date2 < last_monday)

# update where both flags are true
flags = last_monday_flags & date2_flags
df.loc[flags, 'Date2'] = last_monday

# if today is Monday
if day_of_week == 0: 
    last_sunday = last_monday + pd.to_timedelta(6, unit='d')
    last_sat = last_sunday - pd.to_timedelta(1, unit='d')

    last_week_flags = (df.Date >= last_monday) & (df.Date <= next_sunday)

    last_sat_flags = (df.Date2 == last_sat)
    last_sun_flags = (df.Date2 == last_sun)

    # I'm just too lazy and not sure how Sat and Sun relates to Fri
    # but i guess just subtract 1 day or 2 depending on which day
    ...
从日期时间导入日期时间
today=pd.to_datetime(datetime.now().date())
周中的天=今天。周中的天
上周一=今天-pd.至时间增量(一周中的第二天,单位为d)
#如果今天是星期一,我们需要再往后退一周
如果周中的天=0:
最后一个星期一-=pd到时间增量(7,单位为d)
#为上周一过滤
上周一标志=(df.Date==上周一)
#筛选日期2<上周一
date2_标志=(df.date2<上周一)
#更新两个标志均为true的位置
标志=上周一标志和日期2标志
df.loc[flags,'Date2']=上周一
#如果今天是星期一
如果周中的天=0:
last_sunday=last_monday+pd.至时间增量(6,单位=d)
last_sat=last_sunday-pd.至时间增量(1,单位=d)

last_week_flags=(df.Date>=last_monday)和(df.Date),您能否帮助描述针对这种情况应该做什么-要点2的第二步:过滤今天日期之前的星期一的“日期”列[以dt.strftime('%#m/%#d/%Y')格式]你需要像你在帖子中说的那样检查你的数据:
我有一个日期列,而错误告诉你没有。你是对的,很抱歉我的疏忽@quang Hoang
last_monday_flags
确实是一个布尔序列。你可以通过
df[last_monday_flags]
df.loc[last_monday_flags,:]
,您可以在后面的代码中看到。是的,用于您的目的。