86 words
1 minute
Errors When Using pandas.to_datetime with Different Date and Time Formats
https://dreaife-team.atlassian.net/browse/DREAITE-39
While reading RUNOOB’s guide on using pandas to clean incorrectly formatted data, I found that the code it provided would not run with my current version.
After searching online for ages, all the solutions I found involved changing the errors parameter.
Finally, I reread the error message and discovered that changing format to mixed tells pandas that the data contains mixed formats, which solves the problem (sweat). This is probably because my Python 3 version is too new.
Code that causes the error:
import pandas as pd
# 第三个日期格式错误data = { "Date": ['2020/12/01', '2020/12/02' , '20201226'], "duration": [50, 40, 45]}
df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
df['Date'] = pd.to_datetime(df['Date'])
print(df.to_string())Error message:
ValueError: time data "20201226" doesn't match format "%Y/%m/%d", at position 2. You might want to try: - passing `format` if your strings have a consistent format; - passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format; - passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.Fixed code:
import pandas as pd
# 第三个日期格式错误data = { "Date": ['2020/12/01', '2020/12/02' , '20201226'], "duration": [50, 40, 45]}df = pd.DataFrame(data, index = ["day1", "day2", "day3"])
df['Date'] = pd.to_datetime(df['Date'], format='mixed')# df['Date'] = pd.to_datetime(df['Date'],format="%Y/%m/%d",errors='ignore')
print(df.to_string()) Share
If this article helped you, please share it with others!
Errors When Using pandas.to_datetime with Different Date and Time Formats
https://dreaife.tokyo/en/posts/pandas-datetime-fix/ Some information may be outdated
Related Posts Smart
1
Installing Oh My Zsh and Its Plugins on Ubuntu
Troubleshooting Install and configure Oh My Zsh on Ubuntu with Zsh, Git, Powerlevel10k, useful plugins, and .zshrc settings for a productive terminal.
2
Configure Docker + code-server on Alibaba Cloud to Build an Online IDE
Troubleshooting Build a C/C++ development environment on Alibaba Cloud with Docker, Nginx, and code-server, from mirror setup to compiling and running test code.
3
Forwarding Network Traffic to a Router via NAT on Ubuntu
Troubleshooting Share an Ubuntu host network with a router by configuring Netplan, static IP, IP forwarding, DHCP, and NAT rules, then verify client connectivity.
4
CSAPP Chapter 1: A Tour of Computer Systems
Study CSAPP Chapter 1 notes tracing a program from bits and compilation through CPU execution, caches, OS abstractions, processes, and virtual memory.
5
Missing Semester — Class 01
Study Missing Semester shell notes on commands, paths, permissions, pipes, I/O redirection, root privileges, Bash examples, and exercises.
Random Posts Random
1
Brimming with Dreams and Hope, Self-Forgiveness and Reverie—A Sakura Doodle
Perspectives 2022-09-18
2
Interview Algorithm Study 1
Study 2023-08-11
3
Getting Started with VS Code Extension Development
Study 2025-03-13
4
Missing Semester — Class 01
Study 2023-01-11
5
EOA Wallet Signature Verification and Related Topics
Exploration 2026-06-10





