-2
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},
]
xin.chen
  • 964
  • 2
  • 8
  • 24
  • Does this answer your question? [How to select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-to-select-rows-from-a-dataframe-based-on-column-values) – Anshu Chen Mar 20 '20 at 01:38
  • @ Anshu says Reinstate Monica i need use series,not dataframe – xin.chen Mar 20 '20 at 01:41

1 Answers1

0

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}]
xin.chen
  • 964
  • 2
  • 8
  • 24