0

I have a field that normally has a date value, and I would like to check if it's empty.

I've tried this:

np.isnan(row['date'] == True

However, this causes an error. This I used for string field. Is it different for each data type?

Donut
  • 110,061
  • 20
  • 134
  • 146
Datawoman
  • 21
  • 3

2 Answers2

1

Use boolean indexing, do not iterate

# set up a sample frame
df = pd.DataFrame(pd.date_range('2021-01-01', '2021-01-15'), columns=['Date'])
# change some values to null
df.iloc[::4] = np.nan

# boolean indexing to find all null values
df_null = df[df['Date'].isna()]

   Date
0   NaT
4   NaT
8   NaT
12  NaT
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
  • Thanks! I get now the null values. How do I use it in an if statement? If I use it than I get the error: The truth value of a dataframe is ambiguous. I need to learn a lot. – Datawoman Sep 29 '21 at 14:54
  • @Datawoman you do not need to use an if statement if you want to assign values to nulls. If you want to assign values based on a condition use numpy.select, numpy.where or df.where. If you want to assign a value to all nulls you can also use `df['Date'].fillna()` – It_is_Chris Sep 29 '21 at 15:33
  • Thanks, I wil try it. – Datawoman Sep 29 '21 at 15:55
  • It worked by the way! thanks – Datawoman Oct 01 '21 at 06:53
0

NAN stands for Not A Number, which is not appropriate for dates. It seems that checking for null is a better approach.