Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

python - Timestamp format not being matched in pandas to_datetime format specifier

I have a pandas dataframe with some timestamp values in a column. I wish to get the sum of values grouped by every hour.

 Date_and_Time  Frequency
0   Jan 08 15:54:39 NaN
1   Jan 09 10:48:13 NaN
2   Jan 09 10:42:24 NaN
3   Jan 09 20:18:46 NaN
4   Jan 09 12:08:23 NaN

I started off removing the leading days in the column and then typed the following to convert the values to date_time compliant format:

dateTimeValues['Date_and_Time'] = pd.to_datetime(dateTimeValues['Date_and_Time'], format='%b %d %H:%M:%S')

After doing so, I receive the following error:

ValueError: time data 'Jan 08 12:41:' does not match format '%b %d %H:%M:%S' (match)

On checking my input CSV, I can confirm that no column containing the above data are incomplete.

I'd like to know how to resolve this issue and successfully process my timestamps to their desired output format.

question from:https://stackoverflow.com/questions/65864234/timestamp-format-not-being-matched-in-pandas-to-datetime-format-specifier

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you create a self defined lambda function which selects the needed format string. You may have to edit the lambda function:

df = pd.DataFrame({'Date_and_Time':['Jan 08 15:54:39', 'Jan 09 10:48:']})
df
>>>
     Date_and_Time
0  Jan 08 15:54:39
1    Jan 09 10:48:

With one typo in line 1. Now selected the format string for every item with the lambda function.

def my_lambda(x):
    f = '%b %d %H:%M:%S' 
    if x.endswith(':'):
        f = '%b %d %H:%M:'
    return pd.to_datetime(x , format=f)

df['Date_and_Time'] = df['Date_and_Time'].apply(my_lambda)
>>> df
        Date_and_Time
0 1900-01-08 15:54:39
1 1900-01-09 10:48:00

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...