I have a bigger dataframe than what I'm showing here but what I'm trying to do is wherever there is certain value in a series (or even better the whole datarame) to change that value to a None. I need these to be None so I can write the dataframe to a database and it will be recognised as null.
series = (['2014/06/05 13:03:56', '2014/07/23 13:03:56', None, '2014/08/21 13:03:56'])
data = pd.DataFrame(series)
0 2014/06/05 13:03:56
1 2014/07/23 13:03:56
2 None
3 2014/08/21 13:03:56
data = pd.to_datetime(data[0], coerce=True)
data
0 2014-06-05 13:03:56
1 2014-07-23 13:03:56
2 NaT
3 2014-08-21 13:03:56
Name: 0, dtype: datetime64[ns]
data = data.map(str)
data
0 2014-06-05 13:03:56
1 2014-07-23 13:03:56
2 NaT
3 2014-08-21 13:03:56
Name: 0, dtype: object
data.replace(to_replace='NaT', value=None)
0 2014-06-05 13:03:56
1 2014-07-23 13:03:56
2 2014-07-23 13:03:56
3 2014-08-21 13:03:56
Name: 0, dtype: object
In the above example, when I try to replace 'NaT' the dataframe actually fills the value with preceeding value and not None. This won't help as it needs to be None. In the actual dataframe I'm working with this usually throws up a type error telling me that I can't do replace None with method pad. I'm using a datetime series here but really I'll need this for more than just datetime series. It seems like it should be basic functionality with pandas but I can't find an answer.
Thanks, Colin
NaTisn't a string, it's a special "not a time" value, similar toNaNfor floats. So, just changeto_replace='NaT'withto_replace=pd.NaTand it'll do what you're trying to do. But I don't think that what you're trying to do is what you actually want.In [506]: df.replace(pd.NaT, 'None') Out[506]: 0 1401973436000000000 1 1406120636000000000 2 None 3 1408626236000000000 Name: 0, dtype: object'None', which causes a different problem then replacing it with an actualNonewould. Not that either one is what he actually wants, but… Pandas tries to deal withNonevalues in a semi-intelligent way; sometimes they get converted to NaN/NaT/0, sometimes they mean "repeat the last value", etc. But'None'doesn't have any special meaning; it's just "some value that I don't know what to do with, I'd better switch toobject".