AlSub
AlSub

Reputation: 1155

How to properly declare 'NaT' in a python function to be applied on a pandas dataframe?

I am trying to apply a function to a new pandas dataframe column. The function should return something when pandas row is NaT and something else in all other cases.

The input is a datetime64 class column.

Here's my attempt:

import numpy as np 

def some_fun(x):
    if np.isnat(x) == False:
        return 'something'
    else: 
        return 'something else'

df['new_col'] = np.vectorize(some_fun(df['date']))    

Console Output:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

data

    date
#0  NaT
#1  NaT
#2  NaT
#3  NaT
#4  2021-01-17
#5  NaT
#6  NaT
#7  NaT
#8  NaT
#9  NaT

Expected output:

    date         new_col
#0  NaT         something else
#1  NaT         something else
#2  NaT         something else
#3  NaT         something else
#4  2021-01-17  something
#5  NaT         something else
#6  NaT         something else
#7  NaT         something else
#8  NaT         something else
#9  NaT         something else

How could I accomplish this task?

Upvotes: 1

Views: 789

Answers (1)

Mahrad Hanaforoosh
Mahrad Hanaforoosh

Reputation: 546

The below code does what you want. also, I made some changes to your code.

def some_fun(x):
  
  if pd.isnull(x):
    return 'something else'
  else: 
    return 'something'

df['new_col'] = [some_fun(x) for x in df['date']] 

unfortunately, np.isnat() failed in my code. so I used pd.isnull() instead according to this answer. if you think that'll work for you, use np.isnat().

Output:

output

Upvotes: 1

Related Questions