Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a timestamp value to a series with pandas creates an int instead

Tags:

python

pandas

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?

like image 244
jim jarnac Avatar asked May 20 '18 04:05

jim jarnac


People also ask

How do I change Timestamp on pandas?

replace() function is used to replace the member values of the given Timestamp. The function implements datetime. replace, and it also handles nanoseconds.

Is time series supported in pandas?

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 .

What is the difference between Timestamp and datetime in pandas?

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.

How do you convert series to time in python?

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.


1 Answers

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.

To solve the issue:

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
like image 78
U12-Forward Avatar answered Sep 18 '22 02:09

U12-Forward