如何修改 Pandas 中的日期时间索引格式(UTC)?

2023-02-14Python开发问题
15

本文介绍了如何修改 pandas 中的日期时间索引格式(UTC)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个看起来像这样的 df:

I have a df that looks like this:

2015-01-29 08:30:00-05:00  199425  199950  199375  199825                  
2015-01-29 08:45:00-05:00  199825  199850  199650  199800                 
2015-01-29 09:00:00-05:00  199825  199900  199450  199625  

如何删除 -05:00 使其看起来像这样?:

How can I remove the -05:00 so It looks like this?:

2015-01-29 08:30:00  199425  199950  199375  199825                  
2015-01-29 08:45:00  199825  199850  199650  199800                 
2015-01-29 09:00:00  199825  199900  199450  199625  

澄清一下,时间没问题,我不需要对此做任何转换,修改的只是格式,(-05:00)

Just to clarify, the time is fine, I don't need to do any transformation on that, the modification is just the format, (-05:00)

更新:

为了更清楚.-5:00 来自应用此程序

For further clarity. The -5:00 comes out of applying this procedure

eastern = pytz.timezone('US/Eastern')
df.index = df.index.tz_localize(pytz.utc).tz_convert(eastern)

谢谢

推荐答案

这是 2015 年 1 月的一个老问题.但是由于还没有答案(尽管有很多评论),所以这里是 2019 年 10 月的答案.原文提问者可能已经找到了答案,但只是作为未来的参考.

This is an old question from Jan 2015. But since there is no answer yet (although lots of comments), here is an answer in Oct 2019. The original questioner probably found an answer already but just as a reference for the future.

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

import pandas as pd

# create dataframe
df = pd.DataFrame({
    'date_original': ['2015-01-29 08:30:00-05:00', '2015-01-29 08:45:00-05:00', '2015-01-29 09:00:00-05:00'],
    'measurement': [199425, 199825, 199825]
})

# make sure to convert date column to datetime, not string
df['date_original'] = pd.to_datetime(df['date_original'])

print('Original dataframe:')
print(df)
print()

# remove the suffix from the date
df['date_transform'] = pd.to_datetime(df['date_original']).dt.strftime('%Y-%m-%d %H:%M:%S')

print('Transformed dataframe:')
print(df)
print()
df

这篇关于如何修改 Pandas 中的日期时间索引格式(UTC)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
pandas

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10