我在下面有這種日期和時間格式的資料。我認為 '.000Z' 結尾部分是時區的偏移量。對不起,沒有這樣的重復。
2021-01-14T21:00:00.000Z
2021-01-14T22:00:00.000Z
2021-01-14T23:00:00.000Z
2021-01-15T00:00:00.000Z
2021-01-15T02:00:00.000Z
2021-01-15T03:00:00.000Z
2021-01-15T04:00:00.000Z
2021-01-15T05:00:00.000Z
理想情況下,我想按小時和分鐘過濾 df 的行。
我確實通過使用提取了小時/分鐘/秒
pd.to_datetime(df['columnxyz).dt.date
which returns the following as dtype object
21:00:00
22:00:00
23:00:00
00:00:00
01:00:00
02:00:00
03:00:00
etc..
然后我嘗試使用 between_time 過濾具有我需要的時間的行。
df.between_time('10:15', '20:45')
不去!我收到錯誤“索引必須是 DatetimeIndex”我認為早期的 Pandas to_datetime 函式會給我一個 DatetimeIndex,但它似乎只是給了我一個物件。
我很困惑為什么它不起作用。但現在也許我應該將上面的小時/分鐘系列轉換為 str,然后將其拆分,然后將其轉換為 int。雖然我確信有一個更優雅的解決方案!
任何幫助表示贊賞!
uj5u.com熱心網友回復:
# create a column with the datetime type (and not just date part)
df['col']= pd.to_datetime(df['columnxyz'])
# set the index to datetime col
df=df.set_index(['col']
# apply filtering (your code)
df.between_time('10:15', '22:45').reset_index()
col columnxyz
0 2021-01-14 21:00:00 00:00 2021-01-14T21:00:00.000Z
1 2021-01-14 22:00:00 00:00 2021-01-14T22:00:00.000Z
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522200.html
標籤:熊猫日期筛选时间分裂
上一篇:如何在SQL(BigQuery)中獲取上個月的第一天
下一篇:每個月的特定日期范圍
