简体   繁体   English

Python Pandas Dataframe 日期时间范围

[英]Python Pandas Dataframe Datetime Range

Here is my code block:这是我的代码块:

import pandas as pd
import datetime as dt
first_day = dt.date(todays_year, todays_month, 1)

print(first_day)
>2021-02-01

print(type(first_day))
>class 'datetime.date'>

My code runs successfully as below:我的代码成功运行如下:

df = pd.read_excel('AllServiceActivities.xlsx',
                   sheet_name='All Service Activities',
                   usecols=[7, 12, 13]).query(f'Resources.str.contains("{name} {surname}")',
                                              engine='python')

Yet, I also wanna do something like this("Scheduled Start" is my column name):然而,我也想做这样的事情(“预定开始”是我的专栏名称):

df = pd.read_excel('AllServiceActivities.xlsx',
                   sheet_name='All Service Activities',
                   usecols=[7, 12, 13]).query(f'Scheduled Start >= {first_day})',
                                              engine='python')

As you can guess it does not work.你可以猜到它不起作用。

There are solutions such like: Select DataFrame rows between two dates , but I want to use "query" method because I don' t want to pass all of the irrelevant data.有这样的解决方案: Select DataFrame 两个日期之间的行,但我想使用“查询”方法,因为我不想传递所有不相关的数据。

Edit(In order to generate test):编辑(为了生成测试):

dtr = [dt.datetime(2021,1,27,12,0),
dt.datetime(2021,2,3,10,0),
dt.datetime(2021,1,25,9,0),
dt.datetime(2021,1,15,7,59),
dt.datetime(2021,1,13,10,59),
dt.datetime(2021,1,12,13,59),
dt.datetime(2021,1,11,13,59),
dt.datetime(2021,2,2,9,29),
dt.datetime(2021,1,20,7,59),
dt.datetime(2021,1,19,10,59),
dt.datetime(2021,2,1,10,0),
dt.datetime(2021,1,19,7,59),
dt.datetime(2021,1,29,7,59),
dt.datetime(2021,1,28,13,0),
dt.datetime(2021,1,28,10,59),
dt.datetime(2021,1,27,19,30),
dt.datetime(2021,1,27,13,30),
dt.datetime(2021,1,18,17,30),
dt.datetime(2021,1,19,9,0),
dt.datetime(2021,1,18,13,0),
dt.datetime(2021,2,1,14,19),
dt.datetime(2021,1,29,14,30),
dt.datetime(2021,1,14,13,0),
dt.datetime(2021,1,8,13,0),
dt.datetime(2021,1,26,10,59),
dt.datetime(2021,1,25,10,0),
dt.datetime(2021,1,23,16,0),
dt.datetime(2021,1,21,10,0),
dt.datetime(2021,1,18,10,59),
dt.datetime(2021,1,11,13,30),
dt.datetime(2021,1,20,22,0),
dt.datetime(2021,1,20,21,0),
dt.datetime(2021,1,22,19,59),
dt.datetime(2021,1,12,13,59),
dt.datetime(2021,1,21,13,59),
dt.datetime(2021,1,20,10,30),
dt.datetime(2021,1,19,16,59),
dt.datetime(2021,1,19,10,0),
dt.datetime(2021,1,14,9,29),
dt.datetime(2021,1,19,8,53),
dt.datetime(2021,1,18,10,59),
dt.datetime(2021,1,13,16,0),
dt.datetime(2021,1,13,15,0),
dt.datetime(2021,1,12,13,59),
dt.datetime(2021,1,11,10,0),
dt.datetime(2021,1,8,9,0),
dt.datetime(2021,1,7,13,0),
dt.datetime(2021,1,6,13,59),
dt.datetime(2021,1,5,12,0),
dt.datetime(2021,1,10,0,0),
dt.datetime(2020,12,8,13,0),
dt.datetime(2021,1,7,11,10),
dt.datetime(2021,1,6,8,12),
dt.datetime(2021,1,5,10,0),
dt.datetime(2021,1,5,15,15),
dt.datetime(2021,1,4,7,59)]

df1= pd.DataFrame(dtr,columns=['Scheduled Start'])
df2 = df1.query("'Scheduled Start' >= @first_day")

Thanks!谢谢!

Without a reproducible example it's hard to know for sure.没有可重复的示例,很难确定。 But try this.但是试试这个。 It uses the @ character for referencing variables.它使用@字符来引用变量。

df = pd.read_excel(
    'AllServiceActivities.xlsx',
    sheet_name='All Service Activities',
    usecols=[7, 12, 13]) \
      .query('Scheduled Start >= @first_day)')

Firstly, thanks for your guiding me @mullinscr.首先,感谢您指导我@mullinscr。

From here got extra information about date_parser and parse_dates:从这里获得了有关 date_parser 和 parse_dates 的额外信息:

https://www.programcreek.com/python/example/101346/pandas.read_excel https://www.programcreek.com/python/example/101346/pandas.read_excel

date_parser is a specific parser function for my cases. date_parser 是针对我的案例的特定解析器 function 。

date_parser = lambda x: pd.datetime.strptime(str(x).split(".")[0], "%Y-%m-%d %H:%M:%S") if str(x).__contains__(".") else (pd.datetime.strptime(str(x), "%Y-%m-%d %H:%M:%S") if not str(x).__contains__("1899") else None)


df = pd.read_excel('AllServiceActivities.xlsx', sheet_name='All Service Activities', header=None, names=["Resources", "Start", "End"], skiprows=1, usecols=[7, 12, 13], parse_dates=[1], date_parser=date_parser).query("Start >= @first_day and End <= @last_day and Resources.str.contains('{} {}')".format(name, surname), engine='python')

Hope helps everyone:).希望对大家有所帮助:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM