pandas 如何在panda Dataframe 中格式化包括pd.NaT在内的时间戳?

jvlzgdj9  于 2022-12-28  发布在  其他
关注(0)|答案(1)|浏览(94)

输出以下错误:

f'{pd.NaT:%Y}'

我本以为它会返回某种NaT或类似于np.nan行为的东西:

f'{np.nan:.2f}'  # this returns 'nan'
qnyhuwrf

qnyhuwrf1#

具有以下玩具 Dataframe :

import numpy as np
import pandas as pd

df = pd.DataFrame(
    np.random.randn(5, 2),
    index=["a", "b", "c", "d", "e"],
    columns=["one", "two"],
)
df["timestamp"] = pd.Timestamp("20120101")
df.loc[["a", "b", "e"], ["timestamp"]] = pd.NaT

下面是一种方法:

df["year"] = df["timestamp"].apply(lambda x: f"{x:%Y}" if x is not pd.NaT else str(x))

然后:

print(df)
# Output
        one       two  timestamp  year
a  0.310610 -1.882089        NaT   NaT
b -0.130122  0.633239        NaT   NaT
c  0.251551  1.590384 2012-01-01  2012
d -1.635151 -0.123938 2012-01-01  2012
e  1.925423  1.142137        NaT   NaT

相关问题