I have a dataframe as below and would like to convert all of the UNIX times to normal date format only for the filtered int values
ID | State | TYPE | START DATE | END DATE | |
---|---|---|---|---|---|
0 | 863 | MI | Hire | 2023-09-01 | 2023-12-31 |
1 | 224 | WI | Retire | 2023-09-01 | 2023-12-31 |
2 | 567 | FL | Transfer | 2023-10-01 | 1698710400000000000 |
3 | 345 | NC | NaN | NaT | None |
4 | 432 | NY | NaN | NaT | None |
I tried:
df["END DATE"] = pd.to_datetime(df["END DATE"], unit = 'ns')
but get this error:
ValueError: mixed datetimes and integers in passed array
I filtered out the int
values using df.loc[df["END DATE"].apply(lambda x : isinstance(x, int))]
but am unable to figure out how to apply this with pd.to_datetime
import pandas as pd
import numpy as np
data = {'ID': {0: 863, 1: 224, 2: 567, 3: 345, 4: 432},
'State': {0: 'MI', 1: 'WI', 2: 'FL', 3: 'NC', 4: 'NY'},
'TYPE': {0: 'Hire', 1: 'Retire', 2: 'Transfer', 3: np.nan, 4: np.nan},
'START DATE': {0: pd.Timestamp('2023-09-01 00:00:00'), 1: pd.Timestamp('2023-09-01 00:00:00'), 2: pd.Timestamp('2023-10-01 00:00:00'), 3: pd.NaT, 4: pd.NaT},
'END DATE': {0: '2023-12-31', 1: '2023-12-31', 2: '1698710400000000000', 3: np.nan, 4: np.nan}}
df = pd.DataFrame(data)