Pandas DataFrame是带有标签轴(行和列)的二维大小可变的,可能是异构的表格数据结构。算术运算在行和列标签上对齐。可以将其视为Series对象的dict-like容器。这是 Pandas 的主要数据结构。

Pandas DataFrame.tz_convert()用于将tz-aware轴转换为目标时区。

用法: DataFrame.tz_convert(tz, axis=0, level=None, copy=True)

参数:

tz:字符串或pytz.timezone对象

axis:转换轴

level:如果轴为MultiIndex,则转换特定级别。否则必须为None

copy:同时复制基础数据

返回:tz转换的数据帧

范例1:采用DataFrame.tz_convert()函数转换给定数据帧的时区。

# importing pandas as pd

import pandas as pd

# Creating the DataFrame

df = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],

'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],

'Age':[14, 25, 55, 8, 21]})

# Create the index

index_ = pd.date_range('2010-10-09 08:45', periods = 5, freq ='H', tz = 'US / Central')

# Set the index

df.index = index_

# Print the DataFrame

print(df)

输出:

现在我们将使用DataFrame.tz_convert()函数将给定 DataFrame 的时区转换为“欧洲/柏林”。

# Let's find out the current timezone

# of the given dataframe

print(df.index)

# Let's convert the timezone of the

# dataframe to 'Europe / Berlin'

df = df.tz_convert(tz = 'Europe / Berlin')

# Let's find out the current timezone

# of the given dataframe

print(df.index)

输出:

正如我们在输出中看到的,DataFrame.tz_convert()函数已成功将给定数据帧的时区转换为所需的时区。

范例2:采用DataFrame.tz_convert()函数转换给定数据帧的时区。给定数据帧的索引是一个MultiIndex。

# importing pandas as pd

import pandas as pd

# Creating the DataFrame

df = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],

'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],

'Age':[14, 25, 55, 8, 21]})

# Create the MultiIndex

index_ = pd.MultiIndex.from_product([['Date'], pd.date_range('2010-10-09 08:45', periods = 5, freq ='H', tz =

'US/Central')], names =['Level 1', 'Level 2'])

# Set the index

df.index = index_

# Print the DataFrame

print(df)

输出:

现在我们将使用DataFrame.tz_convert()函数可将给定 DataFrame 中的MultiIndex级别1的时区转换为“欧洲/柏林”。

# Let's find out the current timezone

# of the Level 1 of the given dataframe

print(df.index[1])

# Let's convert the timezone of the

# level 1 of the dataframe to 'Europe / Berlin'

df = df.tz_convert(tz = 'Europe/Berlin', level = 1)

# Let's find out the current timezone

# of the level 1 of the given dataframe

print(df.index[1])

输出:

正如我们在输出中看到的,DataFrame.tz_convert()函数已成功将给定数据帧中所需级别的时区转换为所需时区。

python中convert函数用法_Python Pandas DataFrame.tz_convert用法及代码示例相关推荐

  1. python中change的用法_Python Pandas dataframe.pct_change()用法及代码示例

    Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas dataframe.pct ...

  2. python中mean的用法_Python Pandas dataframe.mean()用法及代码示例

    Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas dataframe.mea ...

  3. python boxplot用法_python pandas DataFrame.boxplot用法及代码示例

    从DataFrame列制作箱形图. 从DataFrame列制作box-and-whisker图,可以选择按其他一些列进行分组.箱形图是一种通过四分位数以图形方式描绘数字数据组的方法.该框从数据的Q1四 ...

  4. python中fillna函数_在pandas dataframe中写一个用户定义的fillna函数,用条件填充np.nan不同的值...

    考虑以下pandas数据帧: import pandas as pd change = [0.475, 0.625, 0.1, 0.2, -0.1, -0.75, 0.1, -0.1, 0.2, -0 ...

  5. python open 打开是什么类型的文件-详解Python中open()函数指定文件打开方式的用法...

    文件打开方式 当我们用open()函数去打开文件的时候,有好几种打开的模式. 'r'->只读 'w'->只写,文件已存在则清空,不存在则创建. 'a'->追加,写到文件末尾 'b'- ...

  6. python中sort函数源代码_Python中sort和sorted函数代码解析

    Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...

  7. python中format函数作用_python中format函数什么意思

    python中format函数什么意思? Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % ...

  8. python中columns是什么意思_Python Pandas DataFrame.columns用法及代码示例

    Pandas DataFrame是带有标签轴(行和列)的二维大小可变的,可能是异构的表格数据结构.算术运算在行和列标签上对齐.可以将其视为Series对象的dict-like容器.这是 Pandas ...

  9. python convert函数_Python pandas.DataFrame.tz_convert函数方法的使用

    DataFrame.tz_convert(tz, axis=0, level=None, copy=True)[source] 将tz-aware axis转换为目标时区. 参数:tz:str或 tz ...

最新文章

  1. vb.net写一个简易的RSS阅读器
  2. 【转】Tomcat7启动的总过程 (有时间自己写下tomcat8的)
  3. AliExpress:在检索式问答系统中应用迁移学习 | PaperDaily #24
  4. cocos2dx Action动作解析(1)
  5. Highcharts数据可视化工具功能效果图详解
  6. 如何隐藏UITabBar 已经如何使已经隐藏的TabBar重现(原创)
  7. 使用ZipEntry解压zip文件报错: java.lang.IllegalArgumentException: MALFORMED
  8. 高分三号卫星GF-3极化SAR
  9. matlab二重定积分_怎么用matlab计算这个二重积分如何用matlab求二重积分
  10. 100以内的平方数记忆法
  11. 制作Win10系统安装U盘和安装纯净版Win10的通用教程
  12. Matlab通过整除判断倍数
  13. CSS实现div的嵌套居中显示的多种方式
  14. linux下Local Adress(本地ip:端口)和Foreign Address(外部ip:端口)
  15. 电池pack结构_电池PACK是什么?锂电池包PACK结构设计需要考虑哪些因素?
  16. 【Java基础】控制台打印日历
  17. vsftpd安装和配置
  18. 电缆标识牌怎么填写规范清晰不褪色
  19. 平板触控笔哪种好?主动式电容笔推荐
  20. python高斯求和1-500

热门文章

  1. 进入虚拟化服务器,服务器虚拟化即将进入3.0时代
  2. php页面不断弹出值_电脑自动弹出网页怎么办
  3. matlab函数冒号代表的意思,MATLAB中冒号运算符的含义
  4. cmd代码表白_手把手教你把Python代码转成exe
  5. python开发上位机软件-UR机器人通信--上位机通信(python)
  6. Win11系统如何调节字体大小
  7. Windows11怎么关机重启?Windows11的关机键在哪?
  8. 如何解决360浏览器卡死的问题
  9. 腾讯视频主演角色弹幕怎么发
  10. mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法