-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.

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

First, the array needs to be converted into a data frame by specifying the columns. Then the above method will work.
That's right @Santos I assume that you already reading data in dataframe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.