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
Installing Oh My Zsh and Its Plugins on Ubuntu
cs-base To install Oh My Zsh and its components on Ubuntu, first install Zsh and Git, then install Oh My Zsh using wget. Next, clone the Powerlevel10k theme and required plugins, and update the .zshrc file to enable them. Finally, install additional plugins such as zsh-bat and you-should-use, and update the system to ensure everything works properly.
2
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.
3
Forwarding Network Traffic to a Router via NAT on Ubuntu
cs-base Configure Netplan, enable IP forwarding, and set up NAT to allow Ubuntu to share its network connection with a router. The process includes clearing existing configurations, assigning a static IP address, installing and configuring a DHCP service, and verifying that network sharing works correctly.
4
CSAPP Chapter 1: A Tour of Computer Systems
cs-base A computer system consists of hardware and system software and runs applications through a program life cycle of creation, execution, output, and termination. Information is made of bits and context, and programs are transformed into executables by the compilation system. The processor reads and executes instructions, using caches to improve performance. The operating system manages hardware and provides abstractions such as processes and virtual memory to support concurrency and parallelism. Abstraction is a core concept in computer science, and virtual machines provide an abstraction of an entire computer.
5
missing-semester-class01
cs-base Introduces the basic features and usage of the shell, including running programs, navigating paths, managing file permissions, redirecting input and output streams, and administering root privileges. It also provides multiple Bash command examples and exercises to reinforce the material.

Table of Contents