【发布时间】:2021-04-02 14:49:49
【问题描述】:
我的数据框 df 是 postgres 表的结果。如果行符合以下条件,我想应用 lambda 函数,以便在 date_ 为 timestamptz 并使用时间戳强制转换时删除区域。
table1:
date_ = up_1
2013-09-27 14:19:46.825000+02:00
2017-03-28 10:10:10.305000+01:00
2019-05-10 10:10:10
table2:
date_ = up_2
2013-09-27 14:19:46.825000+02:00
None
2019-05-10 09:10:00
**expected output**
table 1:
2013-09-27 14:19:46
2017-03-28 10:10:10
2019-05-10 10:10:10
table 2:
2013-09-27 14:19:46
None
2019-05-10 09:10:00
我收到错误 E TypeError: 'tzinfo' is an invalid keyword argument for this function。谢谢你。 请问我该如何更正下面的代码? 我的提议:
if self.date_ in df.columns and df[str(self.date_)].dtypes== 'object' \
and df[str(self.date_)][df[str(self.date_)] != "None"]:
df[str(self.date_)] = df[str(self.date_)].apply(lambda x: x if pd.isnull(x) else
x.replace(tzinfo=None))
【问题讨论】: