简体   繁体   English

出现错误“ValueError:时间数据''与格式'%Y-%m-%d %H:%M:%S'不匹配”

[英]Getting error "ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'"

Here is a sample of the df:这是 df 的示例:

pId tPS                 tLL                 dZ
129 2019-12-02 15:04:09 2019-12-02 15:06:31 5f723
129 2019-12-02 15:04:15 2019-12-02 15:06:37 5f723
129 2019-12-02 15:05:15 2019-12-02 15:07:37 5f723
129 2019-12-02 15:05:18 2019-12-02 15:07:40 5f723
129 2019-12-02 15:05:24 2019-12-02 15:07:46 5f723

The pID is the ID of a person and I am trying to check the entry, exit and duration time for each ID. pID 是一个人的 ID,我正在尝试检查每个 ID 的进入、退出和持续时间。

Here is the code:这是代码:

from datetime import datetime
stats=df.sort_values(by=['pId', 'tPS', 'tLL'])[['pId', 'tPS', 'tLL', 'dZ']]
pid = ''
enter_t = ''
exit_t = ''

enter_exit_times=[]

for ind, row in stats.iterrows():

    if pid =='':
        enter_t = row['tPS']
        print(enter_t)

    if row['pId']!= pid or ((datetime.strftime(row['tLL'], "%Y-%m-%d %H:%M:%S") 
                         - datetime.strftime(exit_t, "%Y-%m-%d %H:%M:%S")).total_seconds()>2*60*60):

    duration = (datetime.strptime(exit_t, "%Y-%m-%d %H:%M:%S") -
                datetime.strptime(enter_t, "%Y-%m-%d %H:%M:%S"))

    enter_exit_times.append([pid, enter_t, exit_t, duration.total_seconds()])

    pid = row['pId']

    enter_t = row['tPS']

enter_exit_times.append([pid, enter_t, exit_t])
enter_exit_times_df = pd.DataFrame(enter_exit_times)

So here所以在这里

  • pid is the id pid是 ID
  • enter_t is the entering time enter_t是进入时间
  • exit_t is the exit time exit_t是退出时间
  • tPS is the in time tPS是及时的
  • tLL is the out time. tLL是超时时间。

I am then creating a list for which I am writing a loop below.然后我正在创建一个列表,我正在为其编写一个循环。 Initially, I run it through a for loop where I iterate through the rows of the data frame.最初,我通过for循环运行它,在该循环中遍历数据框的行。 So there are two if loop, one with pid where an empty value means it needs to take the row[tPS] and if not then it has to run through the not loop.所以有两个if循环,一个带有pid ,其中一个空值意味着它需要获取row[tPS] ,如果不是,那么它必须运行 not 循环。 Then I am calculating the duration and then appending the values to the entry-exit times.然后我计算持续时间,然后将值附加到进出时间。

I am getting this error:我收到此错误:

2019-12-02 15:04:09
---------------------------------------------------------------------------
ValueError                           Traceback (most recent callast)
<ipython-input-411-fd8f6f998cc8> in <module>
12     if row['pId']!= pid or ((datetime.strftime(row['tLL'], "%Y-%m-%d %H:%M:%S") 
13                              - datetime.strftime(exit_t, "%Y-%m-%d %H:%M:%S")).total_seconds()>2*60*60):
---> 14         duration = (datetime.strptime(exit_t, "%Y-%m-%d %H:%M:%S") -
15                     datetime.strptime(enter_t, "%Y-%m-%d %H:%M:%S"))
16         enter_exit_times.append([pid, enter_t, exit_t, duration.total_seconds()])

~/opt/anaconda3/lib/python3.7/_strptime.py in _strptime_datetime(cls, data_string, format)
575     """Return a class cls instance based on the input string and the
576     format string."""
--> 577     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
578     tzname, gmtoff = tt[-2:]
579     args = tt[:6] + (fraction,)

~/opt/anaconda3/lib/python3.7/_strptime.py in _strptime(data_string, format)
357     if not found:
358         raise ValueError("time data %r does not match format %r" %
--> 359                          (data_string, format))
360     if len(data_string) != found.end():
361         raise ValueError("unconverted data remains: %s" %

**ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'**

The cause of the error is that exit_t is not set anywhere in the loop.错误的原因是exit_t没有在循环中的任何地方设置。 It is an empty string.它是一个空字符串。 You set it before the loop to exit_t = '' but then it's never set again.您在循环之前将其设置为exit_t = ''但它再也不会设置了。 That's why strptime throws the error here:这就是strptime在这里抛出错误的原因:

>>> datetime.strptime(' ', "%Y-%m-%d %H:%M:%S")
Traceback (most recent call last):
...
  File "/usr/local/Cellar/python/3.7.6/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 359, in _strptime
    (data_string, format))
ValueError: time data ' ' does not match format '%Y-%m-%d %H:%M:%S'

The solution is to simply set it properly to "tLL" (if I understand you correctly).解决方案是简单地将其正确设置为"tLL" (如果我理解正确的话)。

But I would like to go further and say that I think you are making the code much much more complicated that how it should be.但是我想更进一步说,我认为您正在使代码变得它应该的复杂得多 My understanding is that you just want to compute the time duration between "tPS" (the in time) and "tLL" (the out time).我的理解是您只想计算"tPS" (输入时间)和"tLL" (输出时间)之间的持续时间。 Since you are already iterating over each row, you just need to assign the values appropriately由于您已经在迭代每一行,您只需要适当地分配值

pid = row['pId']

enter_t_str = row['tPS']  # strings
exit_t_str = row['tLL']   # strings

then convert the datetime strings to datetime objects using strptime然后使用strptime将日期时间字符串转换为日期时间对象

enter_t_dt = datetime.strptime(enter_t_str, "%Y-%m-%d %H:%M:%S")
exit_t_dt = datetime.strptime(exit_t_str, "%Y-%m-%d %H:%M:%S")

then calculate the duration然后计算持续时间

duration = exit_t_dt - enter_t_dt

then finally append it to your list然后最后将它附加到您的列表中

enter_exit_times.append([pid, enter_t_str, exit_t_str, duration.total_seconds()])

There is no need to keep track of the "pId" .无需跟踪"pId"

Here's the full code:这是完整的代码:

stats = df.sort_values(by=['pId', 'tPS', 'tLL'])[['pId', 'tPS', 'tLL', 'dZ']]

pid = ''
enter_t = ''
exit_t = ''
enter_exit_times = []

for ind, row in stats.iterrows():
    pid = row['pId']

    enter_t_str = row['tPS']
    exit_t_str = row['tLL']

    enter_t_dt = datetime.strptime(enter_t_str, "%Y-%m-%d %H:%M:%S")
    exit_t_dt = datetime.strptime(exit_t_str, "%Y-%m-%d %H:%M:%S")
    duration = exit_t_dt - enter_t_dt

    enter_exit_times.append([pid, enter_t_str, exit_t_str, duration.total_seconds()])

enter_exit_times_df = pd.DataFrame(enter_exit_times)
print(enter_exit_times_df)

And the output DataFrame:和输出数据帧:

     0                    1                    2      3
0  129  2019-12-02 15:04:09  2019-12-02 15:06:31  142.0
1  129  2019-12-02 15:04:15  2019-12-02 15:06:37  142.0
2  129  2019-12-02 15:05:15  2019-12-02 15:07:37  142.0
3  129  2019-12-02 15:05:18  2019-12-02 15:07:40  142.0
4  129  2019-12-02 15:05:24  2019-12-02 15:07:46  142.0

If you want to only get the enter/exit times for a particular time period of a day, you could create the datetime objects for the start and end times, and do regular comparison:如果您只想获取一天中特定时间段的进入/退出时间,您可以为开始和结束时间创建datetime对象,并进行定期比较:

>>> dt_beg = datetime(2019,12,2,8,0,0)   #8AM
>>> dt_beg
datetime.datetime(2019, 12, 2, 8, 0)
>>> dt_end = datetime(2019,12,2,10,0,0)  #10AM
>>> dt_end
datetime.datetime(2019, 12, 2, 10, 0)
>>> dt = datetime(2019,12,2,9,34,0)      #9:34AM
>>> dt_beg < dt < dt_end
True
>>> dt = datetime(2019,12,2,14,34,0)     #2:34PM
>>> dt_beg < dt < dt_end
False

So you could add a filter for what to append to enter_exit_times :因此,您可以为要附加到enter_exit_times内容添加过滤器:

if (enter_t_dt > start_dt and exit_t_dt < end_dt):
    enter_exit_times.append(...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:时间数据“ 140120 1520”与格式“%Y-%m-%d%H:%M:%S”不匹配 - ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S' ValueError: 时间数据与格式 &#39;%Y-%m-%d %H:%M:%S.%f&#39; 不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f' ValueError: 时间数据“无”与格式“%Y-%m-%d %H:%M:%S”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S' ValueError: 时间数据 '' 与格式 '%Y-%m-%d %H:%M' 不匹配 - ValueError: time data '' does not match format '%Y-%m-%d %H:%M' ValueError: 时间数据 &#39;&#39; 与格式 &#39;%Y-%m-%dT%H:%M:%S&#39; 不匹配 - ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%S' backtrader 时间列:ValueError:时间数据“0”与格式“%Y-%m-%d %H:%M:%S”不匹配 - backtrader time column : ValueError: time data '0' does not match format '%Y-%m-%d %H:%M:%S' ValueError:时间数据与远程计算机文件上的格式&#39;%Y-%m-%d%H:%M:%S&#39;不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S' on remote machine file ValueError: 时间数据 &#39;2013/05/24 07:00:00&#39; 与格式 &#39;%Y-%m-%d %H:%M:%S&#39; 不匹配 - ValueError: time data '2013/05/24 07:00:00' does not match format '%Y-%m-%d %H:%M:%S' 时间数据与格式 '%Y-%m-%d %H:%M:%S' 不匹配 - time data does not match format '%Y-%m-%d %H:%M:%S' 错误:ValueError:时间数据“ N / A”与格式“%Y-%m-%d”不匹配-Python - Error: ValueError: time data 'N/A' does not match format '%Y-%m-%d' - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM