Converting dates into a specific format in side a CSV

Question:

I am new to python and Iam trying to manipulate some data but it keeps showing me this erro message

 UserWarning: Parsing '13/01/2021' in DD/MM/YYYY format. Provide format or specify infer_datetime_format=True for consistent parsing.
  cache_array = _maybe_cache(arg, format, cache, convert_listlike)

This is my code

import pandas as pd
import matplotlib.pyplot as plt

dataLake = pd.read_csv("datalake - Data lake.csv", parse_dates=["Day"])
dataLake = dataLake.rename(columns={"Day":"day"})
dataLake = dataLake.rename(columns={"Agent":"agent"})

dataLake["day"] = pd.to_datetime(dataLake.day)
print(dataLake.head())
Asked By: Isaac Portes

||

Answers:

In your case you need to set the dayfirst param to true, like this:

pd.to_datetime(dataLake.day, dayfirst=True)

or you can set a format (but you don’t need to in your case), like this:

pd.to_datetime(dataLake.day, format="%d/%m/%y")
Answered By: Bora Varol

Whenever we try to pass date without specifying the format, python tries to throw this warning.
You can follow 2 approaches.

  1. Either pass a format for your date like this:
df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3],'day': [4, 5]})
pd.to_datetime(df)
  1. Or just set infer_datetime_format=True, this is faster. Pandas will try to parse the columns into date format.
dt = pd.to_datetime("29/03/2023", infer_datetime_format=True)
Answered By: Shreya Sharma
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.