This piece of code work good to convert the excel general number into date as long as there is no empty value in the column, when the column contain any empty value then it throw any error that i described below. Could anyone help me out how to ignore the empty cell? on the other hand i also need to keep the empty cell as empty because i don't want to drop them, in the data frame other columns contain some data as well.
df = pd.DataFrame({
"date": ['42369', '', '42371', '42372']
})
df["newDate"] = df["date"].map(lambda x: datetime(*xlrd.xldate_as_tuple(int(x), 0)))
I get this type of error when the column contain any empty value.
Error message:enter code here
invalid literal for int() with base 10: ''
CodePudding user response:
Use indexing to remove bad rows:
xldate_as_tuple = lambda x: datetime(*xlrd.xldate_as_tuple(int(x), 0))
df['newDate'] = df.loc[df['date'] != '', 'date'].map(xldate_as_tuple)
print(df)
# Output
date newDate
0 42369 2015-12-31
1 NaT
2 42371 2016-01-02
3 42372 2016-01-03
