Pandas中的字符串和时间转换与格式化

Pandas 提供了若干个函数来格式化时间。

把字符串转为时间格式

其中,最常用的是 to_datetime() 函数。

可以使用 to_datetime() 函数将一个字符串解析为时间,并指定字符串的格式。例如:

import pandas as pd

# 将字符串 "2022-01-01" 转为时间格式
time = pd.to_datetime("2022-01-01", format="%Y-%m-%d")

print(time)

输出:

<class 'pandas._libs.tslibs.timestamps.Timestamp'>:2022-01-01 00:00:00

把时间格式化为字符串

还可以使用 strftime() 函数将时间格式化为字符串。例如:

import pandas as pd

# 将时间 "2022-01-01 00:00:00" 格式化为字符串
time_str = pd.to_datetime("2022-01-01 00:00:00").strftime("%Y-%m-%d")

print(time_str)

输出:

<class 'str'>:2022-01-01

格式化某一列的时间为字符串

如果想要格式化某一列中的时间,可以使用 pandasto_datetime 函数。

例如,假设你有一个名为 df 的数据,并且你想要格式化其中一列名为 “Date” 的时间列,你可以这样做:

df['Date'] = pd.to_datetime(df['Date'])

这将会将 “Date” 列中的所有时间转换为 Pandas 的时间数据类型。你也可以指定一个特定的时间格式,例如:

df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:%S')

这将会将 “Date” 列中的所有时间按照指定的格式转换为 Pandas 的时间数据类型。

遇到的错误

但是,我在修改以下数据的格式时,想要把listed_date和delisted_date格式化为字符串,出现了错误。

      sec_id  sec_name               listed_date             delisted_date
2716  hc2301  热轧卷板2301 2022-01-18 00:00:00+08:00 2023-01-16 00:00:00+08:00
2717  hc2302  热轧卷板2302 2022-02-16 00:00:00+08:00 2023-02-15 00:00:00+08:00
2718  hc2303  热轧卷板2303 2022-03-16 00:00:00+08:00 2023-03-15 00:00:00+08:00
2719  hc2304  热轧卷板2304 2022-04-18 00:00:00+08:00 2023-04-17 00:00:00+08:00
2720  hc2305  热轧卷板2305 2022-05-17 00:00:00+08:00 2023-05-15 00:00:00+08:00
2721  hc2306  热轧卷板2306 2022-06-16 00:00:00+08:00 2023-06-15 00:00:00+08:00
2722  hc2307  热轧卷板2307 2022-07-18 00:00:00+08:00 2023-07-17 00:00:00+08:00
2723  hc2308  热轧卷板2308 2022-08-16 00:00:00+08:00 2023-08-15 00:00:00+08:00
2724  hc2309  热轧卷板2309 2022-09-16 00:00:00+08:00 2023-09-15 00:00:00+08:00
2725  hc2310  热轧卷板2310 2022-10-18 00:00:00+08:00 2023-10-16 00:00:00+08:00
2726  hc2311  热轧卷板2311 2022-11-16 00:00:00+08:00 2023-11-15 00:00:00+08:00
2727  hc2312  热轧卷板2312 2022-12-16 00:00:00+08:00 2023-12-15 00:00:00+08:00

格式化代码如下:

data_choose['listed_date'] = data_choose['listed_date'].dt.strftime('%Y-%m-%d %H:%M:%S')
data_choose['delisted_date'] = data_choose['delisted_date'].dt.strftime('%Y-%m-%d %H:%M:%S')

报错:

SettingWithCopyWarning:      
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

推测错误原因:

不能直接修改原来的dataframe上的列,然后再替换原来的列数据。

于是,复制了一个数据副本:

data_new = data_choose.copy()

data_new['listed_date'] = data_choose['listed_date'].dt.strftime('%Y-%m-%d %H:%M:%S')
data_new['delisted_date'] = data_choose['delisted_date'].dt.strftime('%Y-%m-%d %H:%M:%S')

在复制的数据上进行修改,果然没再报错。

输出:

      sec_id  sec_name          listed_date        delisted_date
2716  hc2301  热轧卷板2301  2022-01-18 00:00:00  2023-01-16 00:00:00
2717  hc2302  热轧卷板2302  2022-02-16 00:00:00  2023-02-15 00:00:00
2718  hc2303  热轧卷板2303  2022-03-16 00:00:00  2023-03-15 00:00:00
2719  hc2304  热轧卷板2304  2022-04-18 00:00:00  2023-04-17 00:00:00
2720  hc2305  热轧卷板2305  2022-05-17 00:00:00  2023-05-15 00:00:00
2721  hc2306  热轧卷板2306  2022-06-16 00:00:00  2023-06-15 00:00:00
2722  hc2307  热轧卷板2307  2022-07-18 00:00:00  2023-07-17 00:00:00
2723  hc2308  热轧卷板2308  2022-08-16 00:00:00  2023-08-15 00:00:00
2724  hc2309  热轧卷板2309  2022-09-16 00:00:00  2023-09-15 00:00:00
2725  hc2310  热轧卷板2310  2022-10-18 00:00:00  2023-10-16 00:00:00
2726  hc2311  热轧卷板2311  2022-11-16 00:00:00  2023-11-15 00:00:00
2727  hc2312  热轧卷板2312  2022-12-16 00:00:00  2023-12-15 00:00:00

使用apply()和lambda函数

另一种格式化方式,并不会出错:

# 把日期改为字符串
data_choose.listed_date = data_choose.listed_date.apply(lambda x: x.strftime("%Y-%m-%d %H:%M:%S"))
# 把时间列转为字符串
data_choose.delisted_date = data_choose.delisted_date.apply(lambda x: x.strftime("%Y-%m-%d %H:%M:%S"))

这里对列使用了apply()和lambda函数,相当于遍历列的数据进行修改替换。

把某一列转为时间格式

直接使用pd.to_datetime()就可以了。

这里不会出现错误。

# 把列转为时间格式
data_new.listed_date = pd.to_datetime(data_new.listed_date)
data_new.delisted_date = pd.to_datetime(data_new.delisted_date)
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏悠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值