0

I have a dataset whose exact format or origin is unknown to me. I know that the time that the data was recorded is around September 2017, and the time column contains the following numbers:

[736887.61095486],
[736887.61096065],
...,
[736905.61505787],
[736905.61506366],

Any ideas how can I convert this to proper DateTime and use this column as my DataFrame index?

Sheldore
  • 37,862
  • 7
  • 57
  • 71
ValientProcess
  • 1,699
  • 5
  • 27
  • 43
  • To get time from an integer or float you need to know both the origin and the unit of the timestamp. Since you provided neither it's impossible to know if that's the total number of seconds since September 1 2017, or the total number of minutes since May 1 2016, both of which would give you dates in September 2017. – ALollz Sep 14 '18 at 21:48
  • 1
    I imported the data from a mat file - so it might be MATLAB format. Is this helping? Also, I believe that the time is in seconds – ValientProcess Sep 14 '18 at 21:51
  • Yes, that does help. Your timestamps are "the whole and fractional number of days from a fixed, preset date (January 0, 0000) in the proleptic ISO calendar" [source](https://www.mathworks.com/help/matlab/ref/datenum.html). This is a pain to deal with in pandas, because dates are limited, so hopefully someone can provide a solution now that the origin and unit is known. – ALollz Sep 14 '18 at 21:58
  • Possible duplicate of [Converting Matlab's datenum format to Python](https://stackoverflow.com/questions/13965740/converting-matlabs-datenum-format-to-python) – Trenton McKinney Sep 14 '18 at 22:08

1 Answers1

2

Now that we know that your numbers represent the number of days from January 0, 0000, we can use module datetime to reconstruct their calendar values:

import pandas as pd
from datetime import datetime, timedelta
data = ... # Your data
pd.Series(timedelta(d[0]) for d in data) + datetime(1,1,1)
#0   2018-07-13 14:39:46.499903
#1   2018-07-13 14:39:47.000162
#2   2018-07-31 14:45:40.999972
#3   2018-07-31 14:45:41.500221

Apparently, the date January 0, 0000 in Python corresponds to the year #1, month #1, and day #1.

DYZ
  • 55,249
  • 10
  • 64
  • 93