Clean Mixed Excel Date Formats in Pandas with Simple Code
This article walks through handling Excel columns that contain both compact (YYYYMMDD) and full timestamp (YYYY-MM-DD HH:MM:SS) date strings in Pandas, showing how to unify formats by removing hyphens, trimming, and converting them to proper datetime objects with concise code.
Introduction
Hello, I'm PiPi. A recent question in a Python community asked how to handle mixed date formats in an Excel column using Pandas.
Problem
The column contains dates like 20230516 and 2023-02-16 17:45:33, and converting them to a uniform date type caused errors.
Solution
Two approaches were suggested. First, determine the string length and apply different parsing methods. Second, replace hyphens to unify the format to YYYYMMDD, then convert.
Implemented code:
df['日期'] = df['日期'].map(lambda x: x.replace('-', '')[:8])
df['日期'] = pd.to_datetime(df['日期'])This successfully resolved the issue.
Conclusion
The article demonstrated a practical method for cleaning mixed date strings in Pandas, providing clear code and tips for handling similar problems.
Tip: When asking for help, share a small, anonymized sample of the data and include error screenshots or the exact code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
