Even if the suggestion in the comments worked, it is not performative in large dataframes because of the use of apply()
, that although it’s faster than a for index, row in df.iterrows()
; is slower than vectorization. See more details here
Note: In the link I passed, do not look at the solution accepted as an answer, but for the one that received 100 points for the quality of it.
That said, it follows another solution:
Creating Dataframe for Testing
>>> import pandas as pd
>>> df = pd.DataFrame({"A": ['11/4/2021', '12/4/2021', '13/4/2021'], "B": [1, 2, 3], "C": ['1/4/2021', '2/4/2021', '3/4/2021']})
>>> df
A B C
0 11/4/2021 1 1/4/2021
1 12/4/2021 2 2/4/2021
2 13/4/2021 3 3/4/2021
Checking type of columns
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 3 non-null object
1 B 3 non-null int64
2 C 3 non-null object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes
Converting column A
>>> df["A"] = pd.to_datetime(df["A"], format='%d/%m/%Y')
>>> df
A B C
0 2021-04-11 1 1/4/2021
1 2021-04-12 2 2/4/2021
2 2021-04-13 3 3/4/2021
Checking columns again
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 3 non-null datetime64[ns]
1 B 3 non-null int64
2 C 3 non-null object
dtypes: datetime64[ns](1), int64(1), object(1)
Place the error message complete. With the error message of just tapping the eye we already inform you the problem.
– Augusto Vasques
the error that gives is only the same, what is in the post.
– F. Correia
Is missing close one
)
at the end ofDataFrame[col] = DataFrame[col].apply(lambda _: datetime.strptime(_,"%m/%d/%Y, %H:%M:%S")
and you’re using a hole_
which is a value discard object. DoDataFrame[col] = DataFrame[col].apply(lambda d: datetime.strptime(d,"%m/%d/%Y, %H:%M:%S"))
– Augusto Vasques
It was! rsrs That’s what we needed.
– F. Correia
I said just send the error message!. Lie, I had to read the source to find the error, the message did not help much. :)
– Augusto Vasques
KKKKKKKKKKKKK
– F. Correia