Home > Net >  Convert column of Dataframe to time
Convert column of Dataframe to time

Time:12-12

I have the following Dataframe: Dataframe

Now i want to convert the column "ABZEIT" to time format. So the first rows would be:

13:05 15:40 14:20 16:30 07:40 12:05 17:15

CodePudding user response:

pd.to_datetime(df['ABZEIT'])

Pandas to_datetime Control timezone-related parsing, localization and conversion. If True, the function always returns a timezone-aware UTC-localized Timestamp, Series or DatetimeIndex. To do this, timezone-naive inputs are localized as UTC, while timezone-aware inputs are converted to UTC. If False (default), inputs will not be coerced to UTC.

CodePudding user response:

A solution that first defines a to_time() function then apply it to the dataframe by using map().

from datetime import time

def to_time(t: int) -> time:
    t = f"{t:04}"  # add the leading zero if needed
    return time(int(t[:2]), int(t[2:]))

df["ABZEIT"] = df["ABZEIT"].map(to_time)
  • Related