简体   繁体   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?

my data我的数据

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}]

my code我的代码

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')))

but i get error data但我得到错误数据

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

I need last 3 days or 3 years or 3 months or 3 seconds from today: but get 2019-06-05 data我需要从今天开始的最后 3 天或 3 年或 3 个月或 3 秒:但获取 2019-06-05 数据

i hope data is []我希望数据是 []

One idea if need last 3 days from today first filter rows by boolean indexing with today timestamp converted to days (removed times by set to 00:00:00 ) and then use Series.last :一个想法,如果需要从今天开始的最后 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.

相关问题 在python中计算从今天开始过去6个月的天数 - Calculate number of days for last 6months from today in python 如何使用月份的第一天和最后几天创建 pandas 数据框 - how to create a pandas data frame with the first days and the last days of Months 如何获取Pandas DataFrame的最近5个月的数据? - How to get last 5 months of data of a Pandas DataFrame? Pandas 数据框计算过去 3 年相同月份的平均值并附加在同一数据框中 - Pandas data frame calculate avegrage of same months last 3 years and append in the same data frame 在 pandas 中将月份和年份转换为天 - convert months and years into days in pandas 对于数据获取,公司在过去 3 年中的参与者人数增加了 Pandas - For data get the Companies have increasing no of participants in last 3 years in Pandas Python 从日期中减去日期或天数得到年、月、日 - Python subtract dates or days from date to get years, months, days 如何使用熊猫从当前行中获取过去12个月的产品 - How can I get product of last 12 months from the current row using pandas 如何根据日期从 2 年的记录中删除最近 2 个月的记录? - How to remove last 2 months records from 2 years records based on Date? 如何从带有时间戳的数组中获取数据-python中的年,月,日? - How to get from array with Timestamps - years, months, days in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM