Converting a numpy float array to datetime array

Question:

I have a data in which the time are recorded as digits in each columns of a numpy array (dtype= float64) as shown here.

year  month  day    hour
2013     12    3  8.3478
2013     12    3  8.3480
2013     12    3  8.3482
2013     12    3  8.3488
2013     12    3  8.3490
2013     12    3  8.3492

Here the first, second, third, and fourth columns are year, month, day, and hour respectively.

I would like to convert this into datetime format (like ‘%y/%m/%d %H:%M:%S’) by either combining the entries in each columns into a single column or in any other ways.

Asked By: Santos

||

Answers:

Just create a new column and convert "year", "month", "day", and "hour" columns using pd.to_datetime()

df["datetime"] = pd.to_datetime(df[["year", "month", "day", "hour"]])
print(df)

   year  month  day    hour                datetime
0  2013     12    3  8.3478 2013-12-03 08:20:52.080
1  2013     12    3  8.3480 2013-12-03 08:20:52.800
2  2013     12    3  8.3482 2013-12-03 08:20:53.520
3  2013     12    3  8.3488 2013-12-03 08:20:55.680
4  2013     12    3  8.3490 2013-12-03 08:20:56.400
5  2013     12    3  8.3492 2013-12-03 08:20:57.120
Answered By: Jamiu S.