[英]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.