LeCoda
LeCoda

Reputation: 1016

Convert SQL timestamp point to datetime with python

I am trying to convert a created at timestamp made in SQL to a datetime object, or therabouts, with python.

this is the dataset

<bound method NDFrame.to_clipboard of                id                       user_id  sentiment  magnitude
created                                                              
1601820360     10  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.1        0.1
1601820365     11  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.8        0.8
1601900938     12  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.2        0.2
1601900956     13  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -0.2        0.2
1601900971     14  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2        0.2        0.2
...           ...                           ...        ...        ...
1618553420  45024  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618553422  45025  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618553430  45026  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618553432  45027  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0
1618883226  46022  cPL1Fg7BqRXvSFKeU1mJT7KCCTq2       -1.0        1.0

[10506 rows x 4 columns]>

With created being the timestamp in question.

Any idea on best approaches? Having a look through the python documentation i'm a bit lost. Thanks!

Upvotes: 0

Views: 263

Answers (1)

Utsav
Utsav

Reputation: 5918

You might need to use unit as second.

pd.to_datetime(df.created, unit='s')

Sample from your dataset:

Input:

    created     id
0   1601820360  10
1   1601820365  11

Output

     created    id
0   2020-10-04 14:06:00 10
1   2020-10-04 14:06:05 11

Upvotes: 1

Related Questions