我目前有一些日期字符串形式的数据,我想将其标准化为零填充%H:%M:%S字符串。在其原始形式中,数据以下列方式偏离标准格式:
目前,这就是我所拥有的:
df['arrival_time'] = pd.to_datetime(df['arrival_time'].map(lambda x: x.strip()), format='%H:%M:%S').dt.strftime('%H:%M:%S')
但是我在超过24小时的时间内收到错误。有没有一种方法可以将此数据帧列转换为正确的格式?
答案 0 :(得分:2)
我相信你需要:
Gradle Dependency Line Service
com.google.firebase:firebase-core:11.8.0 Analytics
com.google.firebase:firebase-database:11.8.0 Realtime Database
com.google.firebase:firebase-firestore:11.8.0 Cloud Firestore
com.google.firebase:firebase-storage:11.8.0 Storage
com.google.firebase:firebase-crash:11.8.0 Crash Reporting
com.google.firebase:firebase-auth:11.8.0 Authentication
com.google.firebase:firebase-messaging:11.8.0 Cloud Messaging
com.google.firebase:firebase-config:11.8.0 Remote Config
com.google.firebase:firebase-invites:11.8.0 Invites and Dynamic Links
com.google.firebase:firebase-ads:11.8.0 AdMob
com.google.firebase:firebase-appindexing:11.8.0 App Indexing
com.google.firebase:firebase-perf:11.8.0 Performance Monitoring
或者:
df = pd.DataFrame({'arrival_time':['2:05:00','2:05:00','25:00:00'],})
df['arrival_time'] = df['arrival_time'].str.strip().str.zfill(8)
print (df)
arrival_time
0 02:05:00
1 02:05:00
2 25:00:00
或者:
df['arrival_time'] = pd.to_datetime(df['arrival_time'].str.strip(), errors='coerce')
.dt.strftime('%H:%M:%S')
print (df)
arrival_time
0 02:05:00
1 02:05:00
2 NaT