Home > Net >  removing nan from the datetime np.array: array extracted from datetime column with unique values
removing nan from the datetime np.array: array extracted from datetime column with unique values

Time:11-17

I have following list has two values one is datetime.datetime(2018-06-18) and another NaN. both are extracted from the datetime column unique values . I just want list only contain the date.

# extracting date from datetime column
main_df['date'] = main_df.DateTime.dt.date 

# getting only unique values from date column
agg_hos =  main_df['date'].unique()

# output is
array([datetime.date(2018, 6, 18), NaT], dtype=object)

Want to remove the nan from the list tried different ans from site remove nan values from np array

# desired output
array([datetime.date(2018, 6, 18)], dtype=object)

How to do it?

CodePudding user response:

you can use pd.isnull check instead (borrowing from this answer):

import datetime
import numpy as np
import pandas as pd

# np.isfinite(pd.NaT), np.isnan(pd.NaT)
# -> TypeError !

arr = np.array([datetime.date(2018, 6, 18), pd.NaT])

arr = arr[~pd.isnull(arr)]
# array([datetime.date(2018, 6, 18)], dtype=object)
  • Related