0

I have a column of float values which are tweet creation dates. This is the code I used to convert them from float to datetime:

t = 1508054212.0

datetime.utcfromtimestamp(t).strftime('%Y-%m-%d %H:%M:%S')

All the values returned belong to October 2017. However, the data is supposed to be collected over multiple months. So the dates should have different months and not just different Hours, Minutes and Seconds.

These are some values which I need to convert:

  1. 1508054212.0
  2. 1508038548.0
  3. 1506890436.0

Request you to suggest an alternative approach to determine the dates. Thank you.

kanha
  • 3
  • 4

1 Answers1

0

I assumed df['tweet_creation'].loc[1] will return a number like the examples you gave.

Unfortunately, I don't know what f is, but I assumed it was a float.

My answer is inspired by this other answer: Converting unix timestamp string to readable date. You have a UNIX timestamp, so the easiest way is to use it and not convert it as a string.

from datetime import datetime, timedelta

dtobj = datetime.utcfromtimestamp(int(df['tweet_creation'].loc[1])) + timedelta(days=f-int(f))

To have the string representation you can use the function strftime.

Thomas
  • 104
  • 1
  • 6
  • I get an output of the same month and year but different hours, minutes and seconds. The data is spread over multiple months. The year and month (2017-10) are common in all values. Is there any other method to get the correct months? – kanha Aug 04 '22 at 18:20
  • When I test your timestamps in your example, everything is correct. I have checked them on a website and your timestamps are all in the same year and month. – Thomas Aug 04 '22 at 18:25
  • Must be an issue on my end. Thanks for the help! – kanha Aug 04 '22 at 18:31