0

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!

4
  • 1
    try df['created'] = pd.to_datetime(df['created']) Commented Apr 25, 2021 at 9:10
  • says not a dataframe object Commented Apr 25, 2021 at 9:15
  • can you show the output of "df.head()" where df is your dataset Commented Apr 25, 2021 at 9:25
  • That's what I've pated above. So the index is created, then it's id, then userId, then sentiment and magnitude Commented Apr 25, 2021 at 9:27

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm. When i try to do the same thing it doesn't work. how odd.
OH. I had to type pd., not my dataframe. That worked! Thanks!
Great, enjoy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.