78 words
1 minute
About Errors When Using pandas.to_datetime with Different Time Formats
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/ 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
NumPy Study Notes 2
cs-base This article introduces many NumPy features, including bitwise operations, string operations, mathematical functions, statistical functions, sorting and conditional filtering, byte swapping, array copies and views, the matrix library, linear algebra, file input/output, and integration with Matplotlib. It provides detailed function descriptions and sample code to help readers understand and apply various NumPy capabilities.
Random Posts Random
1
Experiment 6: DNS Protocol Analysis and Measurement
cs-base 2022-07-01
2
Personal Crisis Management and Future Income Planning
life 2025-01-23
3
Experiment 7: HTTP Protocol Analysis and Measurement
cs-base 2022-07-01
4
Getting Started with Angular
FRONTEND 2024-11-04
5
Getting Started with Elasticsearch
middle-side 2023-08-13





