簡體   English   中英

pandas 系列最后我想從今天開始獲取過去 3 天、3 年、3 個月或 3 秒的數據?

[英]pandas seris last I want to get data for last 3 days or 3 years or 3 months or 3 seconds from today?

我的數據

data=[{'content': '2018', 'time': 1528186319, 'title': '2018-06-05 16:11:59', 'info': '', 'id': 1}, {'content': '2019', 'time': 1559722345, 'title': '2019-06-05 16:12:25, 'info': '', 'id': 2}, {'content': '017', 'time': 1496650502, 'title': '2017-06-05 16:15:02, 'info': '', 'id': 4}, {'content': '160', 'time': 1465114543, 'title': '2016-06-05 16:15:43', 'info': '', 'id': 5}]

我的代碼

s = pd.Series(data)
s.index =pd.to_datetime([i['time'] for i in self.dt], utc=True, unit='s').tz_convert('Asia/Shanghai')
sort = s.sort_index()
print(list(sort.last('3d')))

但我得到錯誤數據

{'content': '2019', 'time': 1559722345, 'title': '2019-06-05 16:12:25', 'info': '', 'id': 2}

我需要從今天開始的最后 3 天或 3 年或 3 個月或 3 秒:但獲取 2019-06-05 數據

我希望數據是 []

一個想法,如果需要從今天開始的最后 3 天,首先通過boolean indexing過濾行,並將今天的時間戳轉換為天(通過設置為00:00:00刪除時間),然后使用Series.last

today = pd.Timestamp('now').floor('d')

print(list(sort[sort.index <= today].last('3d')))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM