1 回答

TA貢獻1785條經驗 獲得超8個贊
你被你所看到的有點誤導了。Pandas在時間戳上做一個地板,如果我們在給定的一天。00:00:00
因此,當您制作一個
df['time'] = pd.to_datetime(df['time'])
然后進行從字符串到時間的轉換,這是格式,但是僅當天之后的數據不是 時才會顯示。舉個例子:'%Y-%m-%d %H:%M:%S'00:00:00
import pandas as pd
df = pd.DataFrame(['2014/04/28 00:00:00', '2014-04-29', '2014-04-30'], columns=['time'])
print(df)
print(type(df['time'][0]))
外:
time
0 2014/04/28 00:00:00
1 2014-04-29
2 2014-04-30
<class 'str'>
轉換為 :pd.to_datetime()
df['time'] = pd.to_datetime(df['time'])
print(df)
print(type(df['time'][0]))
外:
time
0 2014-04-28
1 2014-04-29
2 2014-04-30
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
print(df['time'][0])
以系列的一個元素為例:
外:
2014-04-28 00:00:00
現在同樣,如果我們的時間戳之一不在:00:00:00
df = pd.DataFrame(['2014/04/28 10:00:00', '2014-04-29', '2014-04-30'], columns=['time'])
print(df)
print(type(df['time'][0]))
外:
time
0 2014/04/28 10:00:00
1 2014-04-29
2 2014-04-30
<class 'str'>
轉換為 :pd.to_datetime()
df['time'] = pd.to_datetime(df['time'])
print(df)
print(type(df['time'][0]))
外:
time
0 2014-04-28 10:00:00
1 2014-04-29 00:00:00
2 2014-04-30 00:00:00
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
添加回答
舉報