简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM