我有一個(gè)帶有字符串的列,用于轉(zhuǎn)換為日期時(shí)間(西班牙語(yǔ)日期格式)>>> df['Date'].head()0 31/10/20191 31/10/20192 30/10/20193 30/10/20194 29/10/2019Name: Date, dtype: object轉(zhuǎn)換>>>pd.to_datetime(df['Date'], dayfirst = True)>>>df['Date'].head()0 2019-10-311 2019-10-312 2019-10-303 2019-10-304 2019-10-29Name: Date, dtype: datetime64[ns]現(xiàn)在我想按日期對(duì)它進(jìn)行排序,輸出奇怪地轉(zhuǎn)換為:>>>df['Date'] =df.sort_values(by=['Date'], ascending = True)>>>df['Date'].head()0 9443248_191 9443205_192 9441864_193 9441809_194 9440310_19Name: Date, dtype: object任何線索,這里發(fā)生了什么?為什么類(lèi)型轉(zhuǎn)換回對(duì)象?
1 回答

婷婷同學(xué)_
TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
確保您的“日期”列已轉(zhuǎn)換為第一個(gè),然后排序應(yīng)該可以正常工作:datetime
import pandas as pd
df = pd.DataFrame({'Date': ['31/10/2019', '31/10/2019', '30/10/2019', '30/10/2019', '29/10/2019']})
df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
df.sort_values(by='Date', ascending=True, inplace=True)
# df['Date']
# 4 2019-10-29
# 2 2019-10-30
# 3 2019-10-30
# 0 2019-10-31
# 1 2019-10-31
# Name: Date, dtype: datetime64[ns
添加回答
舉報(bào)
0/150
提交
取消