简体   繁体   中英

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

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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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