I’m trying to run fillna
on a column of type datetime64[ns]. When I run something like:
df['date'].fillna(datetime("2000-01-01"))
I get:
TypeError: an integer is required
Any way around this?
Advertisement
Answer
This should work in 0.12 and 0.13 (just released).
@DSM points out that datetimes are constructed like: datetime.datetime(2012,1,1)
SO the error is from failing to construct the value the you are passing to fillna
.
Note that using a Timestamp
WILL parse the string.
JavaScript
x
20
20
1
In [3]: s = Series(date_range('20130101',periods=10))
2
3
In [4]: s.iloc[3] = pd.NaT
4
5
In [5]: s.iloc[7] = pd.NaT
6
7
In [6]: s
8
Out[6]:
9
0 2013-01-01 00:00:00
10
1 2013-01-02 00:00:00
11
2 2013-01-03 00:00:00
12
3 NaT
13
4 2013-01-05 00:00:00
14
5 2013-01-06 00:00:00
15
6 2013-01-07 00:00:00
16
7 NaT
17
8 2013-01-09 00:00:00
18
9 2013-01-10 00:00:00
19
dtype: datetime64[ns]
20
datetime.datetime
will work as well
JavaScript
1
14
14
1
In [7]: s.fillna(Timestamp('20120101'))
2
Out[7]:
3
0 2013-01-01 00:00:00
4
1 2013-01-02 00:00:00
5
2 2013-01-03 00:00:00
6
3 2012-01-01 00:00:00
7
4 2013-01-05 00:00:00
8
5 2013-01-06 00:00:00
9
6 2013-01-07 00:00:00
10
7 2012-01-01 00:00:00
11
8 2013-01-09 00:00:00
12
9 2013-01-10 00:00:00
13
dtype: datetime64[ns]
14