How to find only epoch time present column from the dataframe using python pandas

Viewed 116

I have a dataframe

data = [['true', 'false',-1496188800000], ['false','true', 829008000000], ['true', 'false',],['true','true',1028678400000]]


df = pd.DataFrame(data, columns=['isconnect', 'isactive','datetime'])

I want to find which column is epoch time and get the entire column to convert human-readable date using python pandas.

Note: DateTime column name sometime will be changed.

I tried this code but not working.

                for i in df.column.values:
                    if df.i.str.match(r'^[-+]?!*([0-9]!*){10,}$'):
                        df[i] = datetime.utcfromtimestamp(i / 1000).strftime('%Y-%m-%d') + ' UTC'

 isconnect isactive      datetime
0      true    false -1.496189e+12
1     false     true  8.290080e+11
2      true    false           NaN
3      true     true  1.028678e+12

output look like this:

  isconnect isactive    datetime
0      true    false  1922-08-04
1     false     true  1996-04-09
2      true    false        None
3      true     true  2002-08-07

thanks, advanced

2 Answers

Try this:

for i in columnheader:
    if (df[i].dtype == object) and pd.to_datetime(df[i], unit='ms', errors='coerce').dropna().tolist():
        df[i] = pd.to_datetime(df[i], unit='ms', errors='coerce')
        

The fact is that in your example, there is no valid date in the datetime column. I changed your example by adding a real unix-timestamp. Now it works:

# 1630226430 - real Unix Timestasmp
data = [['true', 'false',-1496188800000], ['false','true', 1630226430], ['true', 'false',],['true','true',1028678400000]]


df = pd.DataFrame(data, columns=['isconnect', 'isactive','datetime'])
for col in df.columns:
    df[col] = pd.to_datetime(df[col], unit='s', errors='ignore')

df
    isconnect   isactive    datetime
0   true    false   -1496188800000.0
1   false   true    2021-08-29 08:40:30
2   true    false   NaT
3   true    true    1028678400000.0
Related