TransWikia.com

Succinctly calculate differences between all datetime columns in a row

Stack Overflow Asked by visionary_20 on August 1, 2020

I have implemented the code to identify the Datetime columns and convert them into DateTime.

def func(df):
    for col in df.columns:
        if df[col].dtype == 'object':
            try:
                df[col] = pd.to_datetime(df[col])
            except ValueError:
                pass
        if df[col].dtype == 'datetime64[ns]':
            df = df.diff(axis=1)
            return df

I want to make new columns using these date columns which are the difference between these columns taking 2 at a time. (for instance if there is date1, date2, date3 columns, output should be like date1-date2, date2-date3, date1-date3). How can it be implemented? I have tried using diff() but I need a concise answer?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP