0

I am trying to run this code.

raw_data['Date'] =  pd.to_datetime(raw_data['UNIX Timestamp'],origin='1970-01-01', format='%Y-%m-%d %H:%M:%S')

The conversion of the date is incorrect.

Unix Timestamp  Date
1546369086  1/1/1970
1546369125  1/1/1970
1546375732  1/1/1970
1546334013  1/1/1970
1546340971  1/1/1970
1546367135  1/1/1970
1546367286  1/1/1970
1546367295  1/1/1970

However, when i use the below code, it is giving me correct result.

import datetime
timestamp = datetime.datetime.fromtimestamp(1546369086)
print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))


print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))
2019-01-02 00:28:06
ALollz
  • 57,915
  • 7
  • 66
  • 89
shreeja7
  • 111
  • 8
  • Please update your question with the result you're getting as well as the result you expect. – Soviut Feb 19 '19 at 15:02
  • This does not make sense. Please provide some input data. the `origin` argument is used when you have numeric values (i.e. number of seconds since some date), while you also provide a format string, indicating you almost certainly don't have a numeric input. – ALollz Feb 19 '19 at 15:02
  • Sorry. Last time it didn't update properly. I have edited the quest. Thanks – shreeja7 Feb 19 '19 at 15:12
  • Do not specify the format, that is used with string dates, you need: `pd.to_datetime(df['Unix Timestamp'], unit='s')`, which is explained in the dup – ALollz Feb 19 '19 at 15:19
  • It still isn't giving the correct date. pd.to_datetime(raw_data['UNIX Timestamp'],unit='ms'). I tried adding origin '1970-01-01' but that also didn't help Out[123]: 0 1970-01-18 21:32:49.086 1 1970-01-18 21:32:49.125 2 1970-01-18 21:32:55.732 3 1970-01-18 21:32:14.013 4 1970-01-18 21:32:20.971 – shreeja7 Feb 19 '19 at 15:26
  • Why are you using `'ms'` as the unit? I specifically posted `'s'` 2 lines above. (Your numbers represent seconds since 1970-01-01, **not** milliseconds since 1970-01-01) – ALollz Feb 19 '19 at 15:32
  • I am getting an error when I'm using 's' – shreeja7 Feb 19 '19 at 15:45
  • OutOfBoundsDatetime: cannot convert input 1546367123676.0 with the unit 's' – shreeja7 Feb 19 '19 at 15:46

0 Answers0