Lambda function for parsing datetime objects in a dataframe


Mon Feb 06 2023 02:43:24 GMT+0000 (Coordinated Universal Time)

Saved by @ktyle #python #pandas

def parsed_df(**kwargs):
    # First define the format and then define the function
    timeFormat = "%Y-%m-%d %H:%M:%S UTC"
    # This function will iterate over each string in a 1-d array
    # and use Pandas' implementation of strptime to convert the string into a datetime object.
    parseTime = lambda x: datetime.strptime(x, timeFormat)
    return pd.read_csv('/path/to/file.csv',parse_dates=['time'], date_parser=parseTime).set_index('time')