my data
data= [{"content": "11", "title": "刘德华", "info": "2020-01-13 08:56:54", "time": 1578877014},
{"content": "22", "title": "刘德", "info": "2020-01-24", "time": 1579877014},
{"content": "33", "title": "apple", "info": "2020-02-28", "time": 1582877014},
{"content": "55", "title": "app", "info": "2020-02-17", "time": 1581877014},
{"content": "66", "title": "appstore", "info": "2019-06-30", "time": 1561877014},
{"content": "44", "title": "banana", "info": "2020-02-28", "time": 1582876014},
{"content": "aa", "title": "banana", "info": "2020-03-12 eee", "time": 1584000882},
{"content": "bb", "title": "Thursday data", "info": "2018-03-12 vvvv", "time": 1520842482},
{"content": "cc", "title": "banana", "info": "2020-03-14 xxx", "time": 1584154305},
{"content": "cc", "title": "banana", "info": "2019-03-14 aa", "time": 1552531905},
{"content": "cc", "title": "Thursday data", "info": "2020-03-19 data", "time": 1584586305},
{"content": "cc", "title": "Thursday data", "info": "2019-11-07 aaa", "time": 1573095105},
]
i want get 2020-01-13 08:00:00 to 2020-01-13 10:56:54 i try use truncate but get is None
s=pd.Series(data,index=pd.to_datetime([i['time'] for i in data],utc=True, unit='s').tz_convert('Asia/Shanghai'))
s.truncate("2020-01-13 08:00:00","2020-01-13 10:56:54").tolist()
>>> []
but use truncate for 2020-01-13 to 2020-01-13 10:56:54 has get data
data],utc=True, unit='s').tz_convert('Asia/Shanghai'))
s.truncate("2020-01-13","2020-01-13 10:56:54").tolist()
>>> [{"content": "11", "title": "刘德华", "info": "2020-01-13 08:56:54", "time": 1578877014}]
params:before and after must be 2020-01-13 10:56:54
I believe you need slicing:
s1 = s["2020-01-13 08:00:00":"2020-01-13 10:56:54"]
print (s1)
2020-01-13 08:56:54+08:00 {'content': '11', 'title': '刘德华', 'info': '202...
dtype: object
I truncate
and got few errors, then I correct them, but I also got no value, I guess it is bug.
s=pd.Series(data,index=pd.to_datetime([i['time'] for i in data],utc=True, unit='s').tz_convert('Asia/Shanghai')).sort_index()
s1 = s.truncate(pd.to_datetime("2020-01-13 08:00:00").tz_localize('utc').tz_convert('Asia/Shanghai'),
pd.to_datetime("2020-01-13 10:56:54").tz_localize('utc').tz_convert('Asia/Shanghai')).tolist()
EDIT: Also checked docs and it explain why working same selecting like "2020-01-13 00:00:00"
and "2020-01-13
:
A truncate() convenience function is provided that is similar to slicing. Note that truncate assumes a 0 value for any unspecified date component in a DatetimeIndex in contrast to slicing which returns any partially matching dates
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다