博客
关于我
理解Python系统下的时间格式
阅读量:336 次
发布时间:2019-03-04

本文共 3102 字,大约阅读时间需要 10 分钟。

  • Overview

    pandas/numpy/datetime/time,这四个module是常用的时间相关模块。timestampdatetimestr是三大类常用的数据类型。需要理顺彼此之间错综复杂的关系。

    The Python world has a number of avaiable representations of dates, times, deltas, and timespans.

  • Native Python dates and times: datetime and dateutil

    Python’s basic objects for working with dates and times reside in the built-in datetime module.

    Third-party dateutil can be used to parse dates from a variety of string formats.

    • The datetime module supplies classes for manipulating dates and times.

    • The dateutil module provides powerful extensions to the standard datetime module.

  • Typed arrays of times: Numpy's datetime64

    The weaknesses of Python’s datetime format inspired the Numpy team to add a set of native time series date type to Numpy.

    The datetime64 dtype encodes dates as 64-bit integers, and thus allows arrays of dates to be represented very compactly.

    The datetime64 requires a very specific input format.

    Because of the uniform type in NumPy datetime64 arrays, this type of operation can be accomplished much more quickly than if we were working directly with Python’s datetime objects.

    • Starting in NumPy 1.7, there are core array date types which natively support datetime functionality. The data type is called “datetime64”, so named because “datetime” is already taken by datetime library included in Python.

      The most basic way to create datetimes is from strings in ISO8601 date or datetime format.

      The Unit for internal storage is :

      1. automatically selected from the form of the string,
      2. and can be either :
        1. a unit: Y M W D
        2. a time unit: h m s ms us ns ps fs as

      datetime64 is the data type; datetime64[ns] or datetime64[s] or datetime64[unit] is datetime64 with unit.

      Finally, we will note that while the datetime64 data type addresses some of the deficiencies of the built-in Python datetime type, it lacks many of the convenient methods and functions provided by datetime and especially dateutil.

  • Dates and times in pandas: best of both worlds

    Pandas builds upon all the tools just discussed to provide Timestamp object, which combines the ease-of-use of datetime and dateutil with the efficient storage and vectorized interface of numpy.datetime64.

    From a group of these Timestamp objects, Pandas can construct a DatetimeIndex that can be used to index data in a Series or DataFrame.

    Pandas Time Series: Indexing by Time

    Where the Pandas time series tools become useful is when you begin to index data by timestamps.

    Pandas Time Series Data Structures

    For timestamps, Pandas provides the Timestamp type: it is essentially a replacement for Python’s native datetime, but is based on the more efficient numpy.datetime64 date type.

    For time Periods, Pandas provides the Period type, based on numpy.datetime64.

    For time deltas or durations, Pandas provides the Timedelta type, based on numpy.timedelta64, more efficient replacement for Python’s native datetime.timedelta type.

  • 汇总

    Python native is datetime.datetime data type from module: datetime;

    更高效的是datetime64 data type from module: NumPy;

    结合上述两者优点的是TimeStamp / Timedelta data type from module: Pandas;

  • 不同数据类型之间的转换

    在这里插入图片描述

  • References

转载地址:http://vtge.baihongyu.com/

你可能感兴趣的文章
Python 使jupyter notebook 从指定浏览器启动 以及编程中途更换浏览器
查看>>
写博客常用的字体颜色(待续)
查看>>
C++ throw、try、catch、noexcept
查看>>
vim之vim滚屏与跳转
查看>>
C指针之函数指针与typedef
查看>>
CentOS8 字体大小调整
查看>>
设计模式之组合模式
查看>>
设计模式之外观模式
查看>>
Linux 验证、数字证书、RPM包中文件的提取
查看>>
《Redis开发与运维》阅读笔记:键管理之单个键管理
查看>>
(CMake):指定标准进行编译、CMake官方文档查看
查看>>
(恋上数据结构笔记):优先级队列(Priority Queue)
查看>>
(Python学习笔记):条件语句
查看>>
(Python学习笔记):字典
查看>>
(C++11/14/17学习笔记):并发基本概念及实现,进程、线程基本概念
查看>>
(C++11/14/17学习笔记):线程启动、结束,创建线程多法、join,detach
查看>>
(C++11/14/17学习笔记):创建多个线程、数据共享问题分析及案例
查看>>
(QT学习笔记):按钮组中的常用控件
查看>>
(音视频学习笔记):SDL-YUV显示-播放音频PCM
查看>>
leetcode 14 最长公共前缀
查看>>