I have a dataframe with date column. Column includes "custom" and "general" datatypes. I want to change it all datetime format. "43891" means "01.03.2020 00:00:00"
TARİH
28.02.2020 00:00:00 -->custom
28.02.2020 00:00:00 -->custom
28.02.2020 00:00:00 -->custom
43891 -->general
43891 -->general
43891 -->general
.
.
Here what I have tried below, same problem with me (ref. changing all dates to standard date time in dataframe)
import pandas as pd
from datetime import datetime, timedelta
def from_excel_ordinal(ordinal, _epoch0=datetime(1899, 12, 31)):
if ordinal >= 60:
ordinal -= 1 # Excel leap year bug, 1900 is not a leap year!
return (_epoch0 + timedelta(days=ordinal)).replace(microsecond=0)
df = pd.read_excel('D:\Documents\Desktop\deneme/deneme1.xlsx', sheet_name='Sheet1')
m = df['TARİH'].astype(str).str.isdigit()
df.loc[m, 'TARİH'] = \
df.loc[m, 'TARİH']\
.astype(int)\
.apply(from_excel_ordinal)
df['TARİH'] = pd.to_datetime(df['TARİH'], errors='coerce')
df.to_excel('D:\Documents\Desktop\deneme/deneme1.xlsx',index=False)
When I apply these codes, I am sharing output below. "General type" cells turns to "NaT".
print(df.loc[3280:3286, 'TARİH'])
Output:
2020-02-28
2020-02-28
2020-02-28
2020-02-28
NaT
NaT
NaT
Name: TARİH, dtype: datetime64[ns]
In this solution, changing all dates to standard date time in dataframe all column is "general" datatype. Due to that problem is solved. But when I apply above codes to my dataframe, Column D format is turning to "datetime" format. Due to that I am taking below error when I run the codes 2nd time:
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [int32]
I will use these codes everyday. Due to that I need solve the format cell problem. I can try also other methods if you offer.
Also I have 3000 rows. So I'm not able to apply manual methods.