pandas - 将 bool 对象转换为列中的 bool 原语

pandas - Convert bool object to bool primitive in a column

df 有一个名为 isInDST 的列,它是 bool 对象类型。

df.isInDST

0          True
1          True
2          True
3          True
4          True
5          True
6          True
7          True
8          True
9          True
...         ...  
849350    False
849351    False
849352    False
849353    False
849354    False
849355    False
849356    False
849357    False
849358    False
849359    False
Name: isInDST, Length: 849360, dtype: object

isInDST 列是从映射函数创建的:

df['isInDST'] = pd.DatetimeIndex(df['time']).tz_localize('UTC').tz_convert('Australia/Victoria').map(lambda x : bool(x.dst().total_seconds()!=0))

我想我的问题是如何编辑 map 函数使 isInDST 成为 bool 原始类型而不是 object?

解决你的问题

df['isInDST'] = df['isInDST'].astype('bool')

更新

df['isInDST'] = pd.DatetimeIndex(df['time']).tz_localize('UTC').tz_convert('Australia/Victoria').map(lambda x : x.dst().total_seconds()!=0)