简体   繁体   中英

pandas series get specified time period

data = [{"content": "11", "title": "aa", "info": "aa 2020-01-13 08:52:54", "time": 1578877014},
        {"content": "22", "title": "bb", "info": "abba 2020-02-14 08:15:54", "time": 1581639354},
        {"content": "8", "title": "affa", "info": "aa 2020-01-15 14:56:54", "time": 1579071414},
        {"content": "1", "title": "dd", "info": "aa 2020-01-16 08:56:54", "time": 1579136214},
        {"content": "5", "title": "ee", "info": "aa 2020-01-16 14:56:54", "time": 1579157814},
        {"content": "33", "title": "cc", "info": "au5a 2020-03-15 08:30:54", "time": 1584231354},
        {"content": "55", "title": "cc", "info": "aa 2020-02-16 17:56:54", "time": 1581847014},
        ]

i want to get 8:00-9:00 all data,unlimited dates, just need to meet the condition of 8: 00-9: 00

[{"content": "11", "title": "aa", "info": "aa 2020-01-13 08:52:54", "time": 1578877014},
{"content": "22", "title": "bb", "info": "aa 2020-02-14 08:15:54", "time": 1581639354},
{"content": "33", "title": "cc", "info": "aa 2020-03-15 08:30:54", "time": 1584231354},
{"content": "1", "title": "dd", "info": "aa 2020-01-16 08:56:54", "time": 1579136214},
]

series

import pandas as pd

s=pd.Series(data,index=pd.to_datetime([i['time'] for i in data],utc=True,  unit='s').tz_convert('Asia/Shanghai'))
print(s.between_time('08:00:00','09:00:00').tolist())

result--->
[{'content': '11', 'title': 'aa', 'info': 'aa 2020-01-13 08:52:54', 'time': 1578877014}, 
{'content': '22', 'title': 'bb', 'info': 'aa 2020-02-14 08:15:54', 'time': 1581639354}, 
{'content': '1', 'title': 'dd', 'info': 'aa 2020-01-16 08:56:54', 'time': 1579136214}, 
{'content': '33', 'title': 'cc', 'info': 'aa 2020-03-15 08:30:54', 'time': 1584231354}]

dataframe code

df=pd.DataFrame(data,index=pd.to_datetime([i['time'] for i in data],utc=True,  unit='s').tz_convert('Asia/Shanghai') )
print(df.between_time('08:00:00', '09:00:00').to_dict('r'))

result--->
[{'content': '11', 'title': 'aa', 'info': 'aa 2020-01-13 08:52:54', 'time': 1578877014}, 
{'content': '22', 'title': 'bb', 'info': 'aa 2020-02-14 08:15:54', 'time': 1581639354},
 {'content': '1', 'title': 'dd', 'info': 'aa 2020-01-16 08:56:54', 'time': 1579136214}, 
{'content': '33', 'title': 'cc', 'info': 'aa 2020-03-15 08:30:54', 'time': 1584231354}]

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