概念理解:

  1. **Series.dt.tz_localize()**方法的作用为:
    官方解释:Localize tz-naive Datetime Array/Index to tz-aware Datetime Array/Index.即,本地化 “tz天真(tz-naive)的日期时间(datetiem)索引/数组” 到 “可感知tz(tz-aware)的日期时间(datetime)索引/数组”。
    Returns:Same type as self,Array/Index converted to the specified time zone.
    详见Pandas官方API reference

那么什么是tz-naive和tz-aware呢?:

There are two kinds of date and time objects: “naive” and “aware”.

An aware object has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation.

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

简单来说,tz-naive没有存储时区的信息,也就是很难用于实际数据分析,而tz-aware表示一个特定地区的时间(存储了时区信息),例如中国(GMT+8), 加拿大(GMT-3, GMT-5, GMT-6, GMT-7)等。

补充知识:
时区是以UTC的偏移量的形式表示的。
1⃣️ GMT(Greenwich Mean Time)格林威治平时
2⃣️DST(Daylight Saving Time)夏令时
3⃣️ UTC(Coodinated Universal Time)协调世界时

  1. **Series.tz_convert()**的作用:

官方解释:Convert tz-aware Datetime Array/Index from one time zone to another.
Parameters:tz str, pytz.timezone, dateutil.tz.tzfile or None
Time zone for time. Corresponding timestamps would be converted to this time zone of the Datetime Array/Index. A tz of None will convert to UTC and remove the timezone information. 即,将tz-aware datetime 从一个时区转移到另一个时区。
Returns:Array or Index
详见Pandas官方API reference

这是我要处理的数据(.csv):

代码如下:

# load data
wireless_df = pd.read_csv("/Users/a123/Desktop/ping_data_lala.csv")
print(wireless_df)# convert timestamps to datetime
wireless_df["timestamp"] = pd.to_datetime(wireless_df["timestamp"], unit="s")
print(wireless_df["timestamp"])# 本地化本地时间 都是Series类型
print("%%%%%%%", "\n", type(wireless_df["timestamp"]))  # 一般时间
wireless_df["timestamp"] = wireless_df.timestamp.dt.tz_localize('UTC')  # 得到naive时间
print("==========", "\n", wireless_df["timestamp"])
wireless_df["timestamp"] = wireless_df["timestamp"].dt.tz_convert("US/Eastern")  # 得到aware时间
print("~~~~~~~~~~", "\n", wireless_df["timestamp"])

运行结果:

   %%%%%%% <class 'pandas.core.series.Series'>
========== 0       2019-05-28 10:55:09+00:00
1       2019-05-28 10:55:20+00:00
2       2019-05-28 10:55:30+00:00
3       2019-05-28 10:55:40+00:00
4       2019-05-28 10:55:50+00:00...
72458   2019-06-04 23:24:19+00:00
72459   2019-06-04 23:24:30+00:00
72460   2019-06-04 23:24:40+00:00
72461   2019-06-04 23:24:50+00:00
72462   2019-06-04 23:25:00+00:00
Name: timestamp, Length: 72463, dtype: datetime64[ns, UTC]
~~~~~~~~~~ 0       2019-05-28 06:55:09-04:00
1       2019-05-28 06:55:20-04:00
2       2019-05-28 06:55:30-04:00
3       2019-05-28 06:55:40-04:00
4       2019-05-28 06:55:50-04:00...
72458   2019-06-04 19:24:19-04:00
72459   2019-06-04 19:24:30-04:00
72460   2019-06-04 19:24:40-04:00
72461   2019-06-04 19:24:50-04:00
72462   2019-06-04 19:25:00-04:00
Name: timestamp, Length: 72463, dtype: datetime64[ns, US/Eastern]

Python Pandas库 Series.dt.tz_localize()和 Series.dt.tz_convert()的简单使用相关推荐

  1. Python pandas库|任凭弱水三千,我只取一瓢饮(6)

    上一篇链接: Python pandas库|任凭弱水三千,我只取一瓢饮(5)_Hann Yang的博客-CSDN博客 DataFrame 类方法(211个,其中包含18个子类.2个子模块) >& ...

  2. Python pandas库|任凭弱水三千,我只取一瓢饮(5)

    上一篇链接: Python pandas库|任凭弱水三千,我只取一瓢饮(4)_Hann Yang的博客-CSDN博客 S~W:  Function46~56 Types['Function'][45: ...

  3. Python pandas库|任凭弱水三千,我只取一瓢饮(4)

    上一篇链接: Python pandas库|任凭弱水三千,我只取一瓢饮(3)_Hann Yang的博客-CSDN博客  R(read_系列2):  Function36~45 Types['Funct ...

  4. Python pandas库|任凭弱水三千,我只取一瓢饮(7)

    上一篇链接: Python pandas库|任凭弱水三千,我只取一瓢饮(6)_Hann Yang的博客-CSDN博客 to_系列函数:22个 (12~22) Function12 to_numpy(s ...

  5. Python pandas库|任凭弱水三千,我只取一瓢饮(1)

    对Python的 pandas 库所有的内置元类.函数.子模块等全部浏览一遍,然后挑选一些重点学习一下.我安装的库版本号为1.3.5,如下: >>> import pandas as ...

  6. Python pandas库|任凭弱水三千,我只取一瓢饮(3)

    上一篇链接: Python pandas库|任凭弱水三千,我只取一瓢饮(2)_Hann Yang的博客-CSDN博客 R(read_系列1):  Function26~35 Types['Functi ...

  7. Python pandas库|任凭弱水三千,我只取一瓢饮(2)

    上一篇链接: Python pandas库|任凭弱水三千,我只取一瓢饮(2)_Hann Yang的博客-CSDN博客 I~Q:  Function10~25 Types['Function'][9:2 ...

  8. python pandas库读取excel/csv中指定行或列数据详解

    通过阅读表格,可以发现Pandas中提供了非常丰富的数据读写方法,下面这篇文章主要给大家介绍了关于python利用pandas库读取excel/csv中指定行或列数据的相关资料,需要的朋友可以参考下 ...

  9. python pandas库——pivot使用心得

    python pandas库--pivot使用心得 2017年12月14日 17:07:06 阅读数:364 最近在做基于python的数据分析工作,引用第三方数据分析库--pandas(versio ...

  10. python | Pandas库导入Excel数据(xlsx格式文件)函数:read_excel()

    导入csv格式文件 python | Pandas库导入csv格式文件函数:read_excel()https://mp.csdn.net/mp_blog/creation/editor/123951 ...

最新文章

  1. php将关联数组输出到前台,如何使用foreach从PHP中的关联数组输出特定数据
  2. 【建站系列教程】2、数据源
  3. start for graduate studies career path
  4. 动态分区分配的“首次适应算法_kafka集群关于资源分配的手册
  5. java 数据字典 spring_springboot+redis+切面实现数据字典功能
  6. k8s源码Client-go中Reflector解析
  7. python入门三:文件操作
  8. 基于vue的网页标尺辅助线工具(vue-ruler-tool)
  9. Win11 Android Stuido虚拟机启动失败、崩溃
  10. Unity3d组合键
  11. 《JAVA: 学习导图》
  12. 第五章 多变量线性回归
  13. 晨控CK-GW06-E03与TwinCAT软件配置指南
  14. 针对手机连WIFI微信公众号等图片加载缓慢问题——解决方案
  15. 学渣的刷题之旅 leetcode刷题 83.删除排序链表中的重复元素
  16. FL Studio 20水果编曲软件中文汉化补丁包(含软件)V2021.20
  17. 压力使人头秃是真的!元凶已被哈佛西湖大学揪出,还给出生发秘诀 | Nature
  18. dw选项卡代码_使用DW软件实现html编码转换的详细步骤
  19. 搜狗输入法如何开启中文简体与繁体切换快捷键
  20. properties中的编码如何生成:例如\u7AD9\u70B9这种

热门文章

  1. 怎样才能在网上快速赚到钱?
  2. (14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动
  3. iOS — 百度地图 使用
  4. 如何等比例调整图片大小?
  5. ■ 直接调用阿里云视频点播API实现视频播放
  6. 【胖虎的逆向之路】Android 7.0 上Magisk配合Xposed的相关问题
  7. Solidity 生成Java类
  8. 电脑突然关机重启是什么原因?
  9. 微信退款服务器系统失败怎么办,微信缴费失败怎么退款?能退回吗?
  10. 申请免费SSL证书(阿里云)