In Python, Pandas:
g = pd.Series(dict(a = 5, b =datetime(2018, 1,1)))
g['datetime'] = pd.Timestamp('2018-01-02')
g
returns:
a 5
b 2018-01-01 00:00:00
datetime 1514851200000000000
dtype: object
Anyone has an idea why the timestamp is converted to its int value here, and how to avoid the problem and properly attach a timestamp to the Series?
replace() function is used to replace the member values of the given Timestamp. The function implements datetime. replace, and it also handles nanoseconds.
Pandas Time Series Data StructuresFor time stamps, Pandas provides the Timestamp type. As mentioned before, it is essentially a replacement for Python's native datetime , but is based on the more efficient numpy. datetime64 data type. The associated Index structure is DatetimeIndex .
Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas. Value to be converted to Timestamp.
to_datetime() Function helps in converting a date string to a python date object. So, it can be utilized for converting a series of date strings to a time series.
I agree with @MrE, as he says:
I think it makes sense: 5 is not a datetime object, so pandas is parsing the data and find that you have both a number and a datetime object, and it parses both as 'object' string, because the dtype needs to be consistent across all data in the Series. The string representation of a datetime is the date string, not timestamp. When you then add to it, you can modify the dtype of a cell
I really agree, it has to find a dtype
and found int
, if you drop the 'a'
index, it will work (this is basically checking if the above is true or not):
g = pd.Series(dict(b =datetime(2018, 1,1)))
g['datetime'] = pd.Timestamp('2018-01-02')
print(g)
Output:
b 2018-01-01
datetime 2018-01-02
dtype: datetime64[ns]
So it is true.
Simply add an extra line at the end:
g['datetime']=pd.to_datetime(g['datetime'])
And now:
print(g)
Is:
a 5
b 2018-01-01 00:00:00
datetime 2018-01-02 00:00:00
dtype: object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With