Arun
Arun

Reputation: 2478

How to convert int to datetime in pandas DataFrame

I have a pandas DataFrame having the following structure:

enter image description here

where time attribute/column represents some point in time from which measurements were taken for a particular animal, denoted by animal_id attribute. The measurements were x and y co-ordinates represented by attributes x and y respectively.

I want to convert time from int to datetime format. But when I do the following:

data['time'] = pd.to_datetime(data['time'])

The output of:

data['time'][:10]

is:

0   1970-01-01 00:00:00.000000001
1   1970-01-01 00:00:00.000000001
2   1970-01-01 00:00:00.000000001
3   1970-01-01 00:00:00.000000001
4   1970-01-01 00:00:00.000000001
5   1970-01-01 00:00:00.000000002
6   1970-01-01 00:00:00.000000002
7   1970-01-01 00:00:00.000000002
8   1970-01-01 00:00:00.000000002
9   1970-01-01 00:00:00.000000002
Name: time, dtype: datetime64[ns]

How do I specify two things in this:

  1. Starting date instead of 1970-01-01 to say 2019-05-10
  2. Specify differences between two consecutive times from the output in above to say differences between minutes

Thanks!

Upvotes: 1

Views: 764

Answers (1)

jezrael
jezrael

Reputation: 863541

Use to_timedelta for minutes timedeltas and add Timestamp, because for minutes to_datetime with parameter origin and unit is not implemented:

data = pd.DataFrame({'time':[1,1,2,2,3,4]})

data['time0'] = pd.to_timedelta(data['time'], unit='Min') + pd.Timestamp('2019-05-10')

data['time1'] = pd.to_datetime(data['time'], origin='2019-05-10', unit='s')
data['time2'] = pd.to_datetime(data['time'], origin='2019-05-10', unit='d')
print (data)

   time               time0               time1      time2
0     1 2019-05-10 00:01:00 2019-05-10 00:00:01 2019-05-11
1     1 2019-05-10 00:01:00 2019-05-10 00:00:01 2019-05-11
2     2 2019-05-10 00:02:00 2019-05-10 00:00:02 2019-05-12
3     2 2019-05-10 00:02:00 2019-05-10 00:00:02 2019-05-12
4     3 2019-05-10 00:03:00 2019-05-10 00:00:03 2019-05-13
5     4 2019-05-10 00:04:00 2019-05-10 00:00:04 2019-05-14

because:

data['time0'] = pd.to_datetime(data['time'], origin='2019-05-10', unit='Min')

ValueError: cannot cast unit Min

Upvotes: 2

Related Questions