简体   繁体   中英

Value Error: time data '12:00:01 AM' does not match format '%I:%M:00 %p' using time.strptime

I'm a bit new to python so any help is greatly appreciated. Thanks in advance (and sorry for any mislabel).

I'm working on a csv file containing columns with Date, Time, CO, CO2 and CH4. What I want to achieve is to make a loop so that every time there is a time with zero seconds (ex: "12:00:00 AM", "3:05:00 PM" etc) it will take the data of that row and send it to a new text or csv file(this part is not included in the code). I imported the csv using pandas and used time.strptime to convert the string to readable time format.

Unfortunately since there is some data missing, I can't make a loop to gather every 60th data for this. I've also tried making a function using strptime but it also gives me a type error saying it must be a string and not a panda core series.

Importing the csv file:

data1 = pd.read_csv("prueba1.csv")
print(data1)

Where the output is:

         DATE         TIME     CO  CO2_dry  CH4_dry
0    3/4/2019  12:00:00 AM  0.352      420     1.99
1    3/4/2019  12:00:01 AM  0.352      420     1.99
2    3/4/2019  12:00:02 AM  0.352      420     1.99
3    3/4/2019  12:00:03 AM  0.366      420     1.99
4    3/4/2019  12:00:04 AM  0.366      420     1.99
5    3/4/2019  12:00:05 AM  0.366      421     1.99
6    3/4/2019  12:00:06 AM  0.369      421     1.99
7    3/4/2019  12:00:07 AM  0.369      421     1.99
8    3/4/2019  12:00:09 AM  0.354      421     1.99
9    3/4/2019  12:00:10 AM  0.354      421     1.99

And the code I'm using is

for i in data1["TIME"]:
        time.strptime(i,"%I:%M:%S %p")
        if time.strptime(i,"%I:%M:%S %p") == time.strptime(i,"%I:%M:00 %p"):
            print("Found a number!", i)
        else:
            print("Yikes")

The error message is:

Found a number! 12:00:00 AM

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-8b936d17df46> in <module>()
      2         time.strptime(i,"%I:%M:%S %p")
      3         #print(i)
----> 4         if time.strptime(i,"%I:%M:%S %p") == time.strptime(i,"%I:%M:00 %p"):
      5             print("Found a number!", i)
      6         else:

C:\Users\Diego\Anaconda3\lib\_strptime.py in _strptime_time(data_string, format)
    557     """Return a time struct based on the input string and the
    558     format string."""
--> 559     tt = _strptime(data_string, format)[0]
    560     return time.struct_time(tt[:time._STRUCT_TM_ITEMS])
    561 

C:\Users\Diego\Anaconda3\lib\_strptime.py in _strptime(data_string, format)
    360     if not found:
    361         raise ValueError("time data %r does not match format %r" %
--> 362                          (data_string, format))
    363     if len(data_string) != found.end():
    364         raise ValueError("unconverted data remains: %s" %

ValueError: time data '12:00:01 AM' does not match format '%I:%M:00 %p'

It returns the preceding output. I expected for it to return all time numbers matching the '%I:%M:00 %p' format, but only returned the first number. It seems odd to me that it stopped after it encountered the first number not matching the specified format.

If you want to skip errors, then you should use try and except

for i in data1["TIME"]:
        try:
           time.strptime(i,"%I:%M:%S %p")
           if time.strptime(i,"%I:%M:%S %p") == time.strptime(i,"%I:%M:00 %p"):
              print("Found a number!", i)
           else:
              print("Yikes")
        except ValueError:
              print("Ouch! Something failed")

You are doing it the long way. pd.to_datetime is both fast and convenient:

df['DATE'] = pd.to_datetime(df['DATE'])
df['TIME'] = pd.to_datetime(df['TIME']).dt.time

Of if your situation calls for a precise format:

df['DATE'] = pd.to_datetime(df['DATE'], format='%m/%d/%Y')
df['TIME'] = pd.to_datetime(df['TIME'], format='%I:%M:%S %p').dt.time

You can get the datetime format specifiers from strftime.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM