0

How do I fillna on a Pandas DataFrame that has a datetime64 dtype? I want the result to be empty string, not a default date.

I thought this would do it. But unfortunately it gives me NaT instead of empty string.

df.date = df.date.fillna('')

Here is full code

import pandas as pd
import numpy as np

np.random.seed(0)
rng = pd.date_range('2015-02-24', periods=5, freq='T')
df = pd.DataFrame({ 'date': rng, 'val': np.random.randn(len(rng)) })

This gives me a dataframe that looks like this

date                val
2015-02-24 00:00:00 1.764052
2015-02-24 00:01:00 0.400157
2015-02-24 00:02:00 0.978738
2015-02-24 00:03:00 2.240893

But say one of the dates is nan

df.at[0, 'date'] = np.nan

I get back this dataframe

date                val
NaT                 1.764052
2015-02-24 00:01:00 0.400157
2015-02-24 00:02:00 0.978738
2015-02-24 00:03:00 2.240893

What I want is this

date                val
                    1.764052
2015-02-24 00:01:00 0.400157
2015-02-24 00:02:00 0.978738
2015-02-24 00:03:00 2.240893

But I don't know how to do it. And this following code unfortunately doesn't work.

df.date = df.date.fillna('') 
user3240688
  • 1,188
  • 3
  • 13
  • 34

1 Answers1

1

It does no make sense to try to fillna a datetime Series with a string, so pandas won't do it.

If you want strings, for instance for display, use:

df['date'] = df['date'].dt.strftime('%Y-%m-%d %H:%M:%S').fillna('')

But you will lose all benefit of the datetime type!

output:

                  date       val
0                       1.764052
1  2015-02-24 00:01:00  0.400157
2  2015-02-24 00:02:00  0.978738
3  2015-02-24 00:03:00  2.240893
4  2015-02-24 00:04:00  1.867558
mozway
  • 194,879
  • 13
  • 39
  • 75