1. <i id='pqht4'><tr id='pqht4'><dt id='pqht4'><q id='pqht4'><span id='pqht4'><b id='pqht4'><form id='pqht4'><ins id='pqht4'></ins><ul id='pqht4'></ul><sub id='pqht4'></sub></form><legend id='pqht4'></legend><bdo id='pqht4'><pre id='pqht4'><center id='pqht4'></center></pre></bdo></b><th id='pqht4'></th></span></q></dt></tr></i><div id='pqht4'><tfoot id='pqht4'></tfoot><dl id='pqht4'><fieldset id='pqht4'></fieldset></dl></div>
          <bdo id='pqht4'></bdo><ul id='pqht4'></ul>
      1. <tfoot id='pqht4'></tfoot>

      2. <small id='pqht4'></small><noframes id='pqht4'>

      3. <legend id='pqht4'><style id='pqht4'><dir id='pqht4'><q id='pqht4'></q></dir></style></legend>

        如何使用空值将字符串转换为日期时间 - python,

        时间:2023-08-04

        <i id='fDev9'><tr id='fDev9'><dt id='fDev9'><q id='fDev9'><span id='fDev9'><b id='fDev9'><form id='fDev9'><ins id='fDev9'></ins><ul id='fDev9'></ul><sub id='fDev9'></sub></form><legend id='fDev9'></legend><bdo id='fDev9'><pre id='fDev9'><center id='fDev9'></center></pre></bdo></b><th id='fDev9'></th></span></q></dt></tr></i><div id='fDev9'><tfoot id='fDev9'></tfoot><dl id='fDev9'><fieldset id='fDev9'></fieldset></dl></div>

          <tbody id='fDev9'></tbody>

          <small id='fDev9'></small><noframes id='fDev9'>

            • <tfoot id='fDev9'></tfoot>
            • <legend id='fDev9'><style id='fDev9'><dir id='fDev9'><q id='fDev9'></q></dir></style></legend>
                <bdo id='fDev9'></bdo><ul id='fDev9'></ul>
                  本文介绍了如何使用空值将字符串转换为日期时间 - python,pandas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个包含一些日期时间(作为字符串)和一些空值作为nan"的系列:

                  I have a series with some datetimes (as strings) and some nulls as 'nan':

                  import pandas as pd, numpy as np, datetime as dt
                  df = pd.DataFrame({'Date':['2014-10-20 10:44:31', '2014-10-23 09:33:46', 'nan', '2014-10-01 09:38:45']})
                  

                  我正在尝试将这些转换为日期时间:

                  I'm trying to convert these to datetime:

                  df['Date'] = df['Date'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
                  

                  但我得到了错误:

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

                  所以我试着把这些变成实际的空值:

                  So I try to turn these into actual nulls:

                  df.ix[df['Date'] == 'nan', 'Date'] = np.NaN
                  

                  然后重复:

                  df['Date'] = df['Date'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
                  

                  然后我得到错误:

                  必须是字符串,不能是浮点数

                  must be string, not float

                  解决这个问题的最快方法是什么?

                  What is the quickest way to solve this problem?

                  推荐答案

                  只要使用to_datetime 并设置 errors='coerce' 来处理 duff 数据:

                  Just use to_datetime and set errors='coerce' to handle duff data:

                  In [321]:
                  
                  df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
                  df
                  Out[321]:
                                   Date
                  0 2014-10-20 10:44:31
                  1 2014-10-23 09:33:46
                  2                 NaT
                  3 2014-10-01 09:38:45
                  
                  In [322]:
                  
                  df.info()
                  <class 'pandas.core.frame.DataFrame'>
                  Int64Index: 4 entries, 0 to 3
                  Data columns (total 1 columns):
                  Date    3 non-null datetime64[ns]
                  dtypes: datetime64[ns](1)
                  memory usage: 64.0 bytes
                  

                  调用 strptime 的问题是如果字符串或 dtype 不正确会引发错误.

                  the problem with calling strptime is that it will raise an error if the string, or dtype is incorrect.

                  如果你这样做了,那么它会起作用:

                  If you did this then it would work:

                  In [324]:
                  
                  def func(x):
                      try:
                          return dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
                      except:
                          return pd.NaT
                  
                  df['Date'].apply(func)
                  Out[324]:
                  0   2014-10-20 10:44:31
                  1   2014-10-23 09:33:46
                  2                   NaT
                  3   2014-10-01 09:38:45
                  Name: Date, dtype: datetime64[ns]
                  

                  但是使用内置的 to_datetime 而不是调用 apply 会更快,这实际上只是循环您的系列.

                  but it will be faster to use the inbuilt to_datetime rather than call apply which essentially just loops over your series.

                  时间

                  In [326]:
                  
                  %timeit pd.to_datetime(df['Date'], errors='coerce')
                  %timeit df['Date'].apply(func)
                  10000 loops, best of 3: 65.8 µs per loop
                  10000 loops, best of 3: 186 µs per loop
                  

                  我们在这里看到使用 to_datetime 的速度提高了 3 倍.

                  We see here that using to_datetime is 3X faster.

                  这篇关于如何使用空值将字符串转换为日期时间 - python,pandas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么 datetime.strptime 在这个简单的例子中不起作 下一篇:pandas read_csv 列 dtype 设置为十进制但转换为字符串

                  相关文章

                  最新文章

                    • <bdo id='lj56G'></bdo><ul id='lj56G'></ul>

                    <small id='lj56G'></small><noframes id='lj56G'>

                    1. <tfoot id='lj56G'></tfoot>
                    2. <i id='lj56G'><tr id='lj56G'><dt id='lj56G'><q id='lj56G'><span id='lj56G'><b id='lj56G'><form id='lj56G'><ins id='lj56G'></ins><ul id='lj56G'></ul><sub id='lj56G'></sub></form><legend id='lj56G'></legend><bdo id='lj56G'><pre id='lj56G'><center id='lj56G'></center></pre></bdo></b><th id='lj56G'></th></span></q></dt></tr></i><div id='lj56G'><tfoot id='lj56G'></tfoot><dl id='lj56G'><fieldset id='lj56G'></fieldset></dl></div>
                      <legend id='lj56G'><style id='lj56G'><dir id='lj56G'><q id='lj56G'></q></dir></style></legend>