When there is a pd.DataFrame type (index contains unix time) as shown in the table below, I want to change the int type timestamp to the datetime type for subsequent processing. If you use the function provided by pandas as it is, the result will be strange, so make a note.
x | y | z | |
---|---|---|---|
timestamp | |||
1450407402547 | -2.153091 | 1.582626 | 9.288803 |
1450407402577 | -2.368118 | 1.287659 | 9.227219 |
1450407402607 | -3.023911 | 4.160522 | 8.133606 |
1450407402637 | -2.316528 | 2.467163 | 9.581879 |
convert.py
df["time"] = pd.to_datetime(df.index , unit="ms")
df["time2"] =df.index
df["time3"] = df.time2.apply(lambda x: datetime.fromtimestamp(x/1000))
x | y | z | time | time2 | time3 | |
---|---|---|---|---|---|---|
timestamp | ||||||
1450407402547 | -2.153091 | 1.582626 | 9.288803 | 2015-12-18 02:56:42.547 | 1450407402547 | 2015-12-18 11:56:42.546999 |
1450407402577 | -2.368118 | 1.287659 | 9.227219 | 2015-12-18 02:56:42.577 | 1450407402577 | 2015-12-18 11:56:42.576999 |
1450407402607 | -3.023911 | 4.160522 | 8.133606 | 2015-12-18 02:56:42.607 | 1450407402607 | 2015-12-18 11:56:42.607000 |
1450407402637 | -2.316528 | 2.467163 | 9.581879 | 2015-12-18 02:56:42.637 | 1450407402637 | 2015-12-18 11:56:42.637000 |
I made time2 by force because an error occurred when I did the following.
df["time3"] = df.index.apply(lambda x: datetime.fromtimestamp(x/1000))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-117-0c598f05317f> in <module>()
----> 1 df["time3"] = df.index.apply(lambda x: datetime.fromtimestamp(x/1000))
AttributeError: 'Int64Index' object has no attribute 'apply'
From the above result, the unix time is converted correctly by the time3 method.
I have to check the specifications of pd.to_datetime ...
Recommended Posts