Precautions when changing unix time to datetime type in pandas

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

Precautions when changing unix time to datetime type in pandas
Precautions when using for statements in pandas
How to handle datetime type in python sqlite3
How to access with cache when reading_json in pandas
Embedding in datetime when only the time is known
Precautions when upgrading TensorFlow (to 1.3)
The minimum methods to remember when aggregating data in Pandas
Save Time type in SQLAlchemy
Precautions when passing def to sorted and groupby functions in Python? ??
What to do when UnicodeDecodeError occurs during read_csv in pandas (pd.read_table ())
Precautions when giving default values to arguments in Python function definitions
What to do when the value type is ambiguous in Python?
How to write soberly in pandas
Misunderstanding due to datetime type subtraction
How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)
Judgment of NaN in pandas: When str type and float type are mixed
Precautions when pickling a function in python
How to reassign index in pandas dataframe
[Python] Pandas to fully understand in 10 minutes
How to read CSV files in Pandas
Adding Series to columns in python pandas
Allow HTML5 <input type = "date / time"> to be used in DatetimeField form in Django
Precautions when adding items using DateField to an existing model later in Django
What to do when "TypeError: data type not understood" appears in python's numpy.zeros