Skip to content Skip to sidebar Skip to footer

Replace Nan Values With The Unique Date (date In Numpy Array.)

I have written a code in a for loop: date = dit[x]['Time'].dt.date.unique() output: [datetime.date(2017, 6, 5)] I am getting a numpy array. I have a dataframe like this: Posit

Solution 1:

.unique() returns a list of all unique elements in the selection (there could be more than one). If you are sure that there will only be one date given your data, just access the first (and only) element of the list. I used a ternary in case the list is empty, in which case it returns a blank string.

.replace(np.nan, str(date[0]) ifdateelse"")

You can probably notice the source of your problem via this:

>>> str([dt.date.today()])
'[datetime.date(2018, 1, 5)]'

Your date variable is a list containing a single date. Converting it to a string results in the output above.

Taking the first element of this list, however, would result in your desired outcome:

>>> str([dt.date.today()][0])
'2018-01-05'

Post a Comment for "Replace Nan Values With The Unique Date (date In Numpy Array.)"