Home > Software engineering >  how to identify which columns in pandas dataframe are only date or only time?
how to identify which columns in pandas dataframe are only date or only time?

Time:03-19

I am writing a function that tries to identify the type of column. Specifically it should be able to tell if the column is only date type or only time type. I tried the below code as per an answer from stack overflow but it fails to identify and always runs into the except block. Below is sample dataframe. I tried running function on Dob column and it returns ''its not a datefield'

enter image description here

def check(col):
    try:
        dt = pd.to_datetime(df[col])

        if (dt.dt.floor('d') == dt).all():
            return ('Its a pure date field')
        elif (dt.dt.date == pd.Timestamp('now').date()).all():
            return ('Its a pure time field')
        else:
            return ('Its a Datetime field') 
    except:
        return ('its not a datefield')

Reproducible input DataFrame :

df = pd.DataFrame({'Name': {0: 'Son', 1: 'Daughter', 2: 'Daughter2', 3: 'Mummy', 4: 'Daddy'}, 'Dob': {0: Timestamp('1986-01-05 00:00:00'), 1: Timestamp('1986-12-09 00:00:00'), 2: Timestamp('1988-07-04 00:00:00'), 3: Timestamp('1968-09-11 00:00:00'), 4: Timestamp('1965-07-02 00:00:00')}, 'Exp': {0: 2.5, 1: 13.5, 2: 24.5, 3: 35.5, 4: 46.5}, 'Time': {0: datetime.time(2, 45), 1: datetime.time(1, 45), 2: datetime.time(1, 45), 3: datetime.time(2, 45), 4: datetime.time(2, 55)}, 'Code': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5}})

CodePudding user response:

It is working fine for me with the provided sample, however I modified the function to take a Series as input to ensure not relying on a global df object.

def check(s):
    try:
        dt = pd.to_datetime(s)
        if (dt.dt.floor('d') == dt).all():
            return ('Its a pure date field')
        elif (dt.dt.date == pd.Timestamp('now').date()).all():
            return ('Its a pure time field')
        else:
            return ('Its a Datetime field') 
    except:
        return ('its not a datefield')


check(df['Dob'])

Output: 'Its a pure date field'

  • Related