-1

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.

mozway
  • 194,879
  • 13
  • 39
  • 75
Santos
  • 3
  • 4

1 Answers1

1

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
Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
  • 1
    First, the array needs to be converted into a data frame by specifying the columns. Then the above method will work. – Santos Jan 21 '23 at 12:12
  • That's right @Santos I assume that you already reading data in dataframe. – Jamiu S. Jan 21 '23 at 12:17