0

I have a df where date is in format dd.mm.yyyy. I want to convert this column into date datatype.

df

Date        Col1   Col2
23.11.2020  V1     100
22.10.1990  V2     200
31.12.1890  V4     79

How to convert date column to date type. Right now Date column is object type.

noob
  • 3,601
  • 6
  • 27
  • 73

2 Answers2

3

Does this solve your question?

pd.to_datetime(df['Date'], format='%d.%m.%Y')

Any other queries on this question are welcome!
Happy Coding!

Aryan
  • 1,093
  • 9
  • 22
2

You can use pandas to_datetime:

import pandas as pd
d = {"Date": ["23.11.2020", "22.10.1990", "31.12.1890"], "Col1": ["V1", "V2", "V4"], "Col2": [100, 200, 79]}
df = pd.DataFrame(d)
df["Date"] = pd.to_datetime(df["Date"], format='%d.%m.%Y', errors='coerce').dt.date
df

    Date    Col1    Col2
0   2020-11-23  V1  100
1   1990-10-22  V2  200
2   1890-12-31  V4  79

df.dtypes

Date    object
Col1    object
Col2     int64
dtype: object
m0nhawk
  • 22,980
  • 9
  • 45
  • 73