gpt4 book ai didi

Python-Pandas-Dataframe-datetime 转换排除空值单元格

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:32 25 4
gpt4 key购买 nike

感谢您花时间查看我的问题。

我尝试使用下面的函数转换 pandas 数据框中的两个日期列。我使用这个函数,因为“结束日期”有 4221 行,所以它不应该在空单元格上崩溃。

最终,更改会生成原始行号的数据帧。因此,我不想丢失在截止日期具有空值的行。

数据框概述:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 4272 entries, 0 to 4271
Data columns (total 4 columns):
Created Date 4272 non-null object
Closed Date 4221 non-null object
Agency 4272 non-null object
Borough 4272 non-null object
dtypes: object(4)

设计的功能:

col='Closed Date'
df[(df[col].notnull())] = df[(df[col].notnull())].apply(lambda x:datetime.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p'))

生成的错误:

TypeError                                 Traceback (most recent call  last)
<ipython-input-155-49014bb3ecb3> in <module>()
9
10 col='Closed Date'
---> 11 df[(df[col].notnull())] = df[(df[col].notnull())].apply(lambda x:datetime.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p'))
12 print(type(df[(df[col].notnull())]))

/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4358 f, axis,
4359 reduce=reduce,
-> 4360 ignore_failures=ignore_failures)
4361 else:
4362 return self._apply_broadcast(f, axis)

/anaconda/lib/python3.6/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
4454 try:
4455 for i, v in enumerate(series_gen):
-> 4456 results[i] = func(v)
4457 keys.append(v.name)
4458 except Exception as e:

<ipython-input-155-49014bb3ecb3> in <lambda>(x)
9
10 col='Closed Date'
---> 11 df[(df[col].notnull())] = df[(df[col].notnull())].apply(lambda x:datetime.datetime.strptime(x,'%m/%d/%Y %I:%M:%S %p'))
12 print(type(df[(df[col].notnull())]))

TypeError: ('strptime() argument 1 must be str, not Series', 'occurred at index Created Date')

最佳答案

我认为你只需要 to_datetime - 它将 NaN 转换为 NaT,因此所有值都是列中的日期时间:

col='Closed Date'
df[col] = pd.to_datetime(df[col], format='%m/%d/%Y %I:%M:%S %p')

示例:

df = pd.DataFrame({'Closed Date':['05/01/2016 05:10:10 AM', 
'05/01/2016 05:10:10 AM',
np.nan]})

col='Closed Date'
df[col] = pd.to_datetime(df[col], format='%m/%d/%Y %I:%M:%S %p')
print (df)
Closed Date
0 2016-05-01 05:10:10
1 2016-05-01 05:10:10
2 NaT

print (df.dtypes)
Closed Date datetime64[ns]
dtype: object

关于Python-Pandas-Dataframe-datetime 转换排除空值单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46069234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com