mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
78 words
1 minute
About Errors When Using pandas.to_datetime with Different Time Formats
2024-01-02

https://dreaife-team.atlassian.net/browse/DREAITE-39

While looking at Runoob’s guide on cleaning formatting errors in pandas, I found that the code Runoob provides doesn’t run on my current version.

Most online resources for this error suggest modifying the errors parameter.

Finally, after re-reading the error message, I realized that changing the format to ‘mixed’—to indicate mixed data formats—solves it (sweat). It seems to be an issue with my Python 3 version being too new.

Error 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'])
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!

About Errors When Using pandas.to_datetime with Different Time Formats
https://dreaife.tokyo/en/posts/pandas-datetime-fix/
Author
dreaife
Published at
2024-01-02
License
CC BY-NC-SA 4.0

Some information may be outdated

Related Posts Smart
1
Pandas Basics
cs-base Pandas is an open-source data analysis library for Python that provides two main data structures, DataFrame and Series, for handling structured data. It supports data cleaning, transformation, analysis, and visualization. After installing Pandas, you can create and operate on Series and DataFrame with simple code, including basic operations, data filtering, and attribute access. Pandas also supports reading and processing CSV and JSON files and provides data cleaning features such as handling missing values and duplicate data.
2
Using Java Thread Pools
cs-base Manually declaring thread pools with ThreadPoolExecutor helps avoid OOM risks, and monitoring thread pool status is recommended, with different businesses using different thread pools. Thread pool parameters should be configured reasonably to avoid repeated creation and long-running tasks, and thread pools should be named to make troubleshooting easier. Pay attention to issues caused by sharing thread pools with ThreadLocal, and consider using TransmittableThreadLocal to solve context propagation problems.
3
Configure Docker + code-server on Alibaba Cloud to Build an Online Compiler
cs-base Set up an online compiler by installing Docker and code-server. The process includes installing Docker, configuring an Alibaba Cloud mirror, running Nginx, installing and configuring code-server, and setting up a C/C++ build environment, then successfully running test code.
4
Experiment 2: IP Protocol Analysis
cs-base This experiment aims to understand the IP packet format and the meaning of its fields, and to master the use of tcpdump and Wireshark. The environment includes an Alibaba Cloud host and operating systems. Through packet capture with tcpdump and analysis with Wireshark, it studies the IP protocol structure and related commands, resolves traceroute and Xftp connection issues, and improves programming ability and understanding of IP.
5
Experiment 7: HTTP Protocol Analysis and Measurement
cs-base This experiment aims to understand the HTTP protocol and its message structure, and to master HTTP packet capture and analysis using tcpdump and Wireshark. By downloading the Xinjiang University homepage, it analyzes the HTTP version, IP addresses, status code, content length, and header fields. A connection error encountered during the experiment was resolved, improving programming skills and understanding of HTTP.

Table of Contents