NumenorForLife
NumenorForLife

Reputation: 1746

Finding All Values in Pandas DataFrame Not of Certain Type

To avoid the following error, I would like to replace any integer in my DataFrame with Unix Time:

ValueError: mixed datetimes and integers in passed array

In a small subset of the Excel files I'm reading in, I know the integers that appear are 0. However, what if there were multiple distinct integers? Or what if there are multiple dtypes? How can I easily replace any non-datetimes with the epoch represented datetime?

This works for the simple case of replacing 0s:

for col_name in time_columns:
    time_col = data[col_name]
    if time_col.dtypes is np.dtype('object'):
        time_col.replace(to_replace=0, value=epoch, inplace=True)
    time_col = pd.DatetimeIndex(time_col).astype(np.int64)/10**6
    data[col_name] = time_col

where

epoch = datetime.datetime.utcfromtimestamp(0)

Upvotes: 1

Views: 1148

Answers (1)

rocky
rocky

Reputation: 7098

Use Python's isinstance() or issubclass()

Upvotes: 1

Related Questions