如何将不同格式的日期时间列转换为特定格式?

发布于 2025-01-13 00:06:04 字数 773 浏览 0 评论 0原文

嗨,我不是 python 专家,我仍然是使用 pandas 和处理数据的初学者。 我有一个带有列时间戳的 df 。列中的日期时间如下所示:

2021-09-07 16:36:14 UTC 
2021-09-04 15:31:44 UTC
2021-07-15 06:49:47.320081 UTC
2021-09-07 14:55:55.353145 UTC

我只想包含日期和时间,末尾没有 UTC 文本,秒后没有小数,最后将数据帧保存在 csv 文件< /强>。基本上我想要这种格式的专栏:

2021-09-07 16:36:14 
2021-09-04 15:31:44
2021-07-15 06:49:47
2021-09-07 14:55:55

我尝试使用这两个函数:

df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S %Z', errors='coerce')
df['timestamp'] = df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S')

我解决了一半的问题。秒后没有小数的日期时间得到修复,但有小数的日期时间只是变空,您可以找到下面的示例:

2021-09-07 16:36:14 
2021-09-04 15:31:44

请问有人可以帮我解决这个问题吗?

Hi I am not an expert in python and I am still a beginner in using pandas and working with data.
I have a df with a column timestamp. The datetime in the column are as shown below:

2021-09-07 16:36:14 UTC 
2021-09-04 15:31:44 UTC
2021-07-15 06:49:47.320081 UTC
2021-09-07 14:55:55.353145 UTC

I would like to have only the date and time, without the UTC text at the end and without the decimals after the second and in the end save the dataframe in a csv file. Basically I want the column in this format:

2021-09-07 16:36:14 
2021-09-04 15:31:44
2021-07-15 06:49:47
2021-09-07 14:55:55

I tried with these two functions:

df['timestamp'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S %Z', errors='coerce')
df['timestamp'] = df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S')

I fix half of the problem. The datetime without the decimals after the second get fixed, but the ones with the decimals just get empty, you can find the example below:

2021-09-07 16:36:14 
2021-09-04 15:31:44

Please can someone help me with this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…红帽 2025-01-20 00:06:04

尝试提取您想要的字段部分。

df['timestamp'] = pd.to_datetime(df['timestamp'].str[:19])
print(df)
print(df.dtypes.

            timestamp
0 2021-09-07 16:36:14
1 2021-09-04 15:31:44
2 2021-07-15 06:49:47
3 2021-09-07 14:55:55


timestamp    datetime64[ns]
dtype: object

Try extracting the part of the field you want.

df['timestamp'] = pd.to_datetime(df['timestamp'].str[:19])
print(df)
print(df.dtypes.

            timestamp
0 2021-09-07 16:36:14
1 2021-09-04 15:31:44
2 2021-07-15 06:49:47
3 2021-09-07 14:55:55


timestamp    datetime64[ns]
dtype: object
彩扇题诗 2025-01-20 00:06:04

你可以取前20个字符:

df['timestamp'] = pd.to_datetime(df['timestamp'].str[:19])
print(df)

# Output
            timestamp
0 2021-09-07 16:36:14
1 2021-09-04 15:31:44
2 2021-07-15 06:49:47
3 2021-09-07 14:55:55

如果你想保留时区信息(UTC),你可以只删除微秒部分:

df['timestamp']= pd.to_datetime(df['timestamp'].str.replace('\.\d+', '', regex=True))
print(df)

# Output
                  timestamp
0 2021-09-07 16:36:14+00:00
1 2021-09-04 15:31:44+00:00
2 2021-07-15 06:49:47+00:00
3 2021-09-07 14:55:55+00:00

You can take the first 20 characters:

df['timestamp'] = pd.to_datetime(df['timestamp'].str[:19])
print(df)

# Output
            timestamp
0 2021-09-07 16:36:14
1 2021-09-04 15:31:44
2 2021-07-15 06:49:47
3 2021-09-07 14:55:55

If you want to keep the timezone information (UTC), you can remove only the microsecond part:

df['timestamp']= pd.to_datetime(df['timestamp'].str.replace('\.\d+', '', regex=True))
print(df)

# Output
                  timestamp
0 2021-09-07 16:36:14+00:00
1 2021-09-04 15:31:44+00:00
2 2021-07-15 06:49:47+00:00
3 2021-09-07 14:55:55+00:00
思念满溢 2025-01-20 00:06:04

尝试 parser,因为它可以采用不同的格式作为输入

from dateutil import parser

# df['timestamp'] = parser.parse(df['timestamp'])
date = parser.parse("2021-07-15 06:49:47.320081 UTC")

print(date)
2021-07-15 06:49:47.320081+00:00

或此输出

# Which would imply
# df['timestamp'] = parser.parse(df['timestamp']).strftime("%F %T")
print(date.strftime("%F %T"))
2021-07-15 06:49:47

Try parser, as it can take different formats as an input

from dateutil import parser

# df['timestamp'] = parser.parse(df['timestamp'])
date = parser.parse("2021-07-15 06:49:47.320081 UTC")

print(date)
2021-07-15 06:49:47.320081+00:00

Or this output

# Which would imply
# df['timestamp'] = parser.parse(df['timestamp']).strftime("%F %T")
print(date.strftime("%F %T"))
2021-07-15 06:49:47
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文