理解Python系统下的时间格式
发布日期:2021-05-07 14:24:35 浏览次数:6 分类:原创文章

本文共 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

上一篇:初识Yandex||ClickHouse
下一篇:理解Library of Congress Cataloging-in-Publication Data

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月02日 03时13分47秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

清华硕士爆料:这些才是机器学习必备的数学基础 2019-03-04
红点中国、红杉中国联合领投,WakeData惟客数据完成1000万美元B轮融资 2019-03-04
看完这篇操作系统,和面试官扯皮就没问题了! 2019-03-04
OpenStack发布Ussuri版本 实现智能开源基础设施的自动化 2019-03-04
整理了一份 Docker系统知识,从安装到熟练操作看这篇就够了 | 原力计划 2019-03-04
2020 AI 产业图谱启动,勾勒中国 AI 技术与行业生态 2019-03-04
“编程能力差,90%输在了数学上!”CTO:多数程序员都是瞎努力! 2019-03-04
霍因科技获首届全国信创产业生态创新奖 2019-03-04
我是程序员,我用这种方式铭记历史 2019-03-04
F5打造“感知可控,随需而变的应用” 助力企业实现非凡数字体验 2019-03-04
CSDN湘苗培优|保持热情,告别平庸 2019-03-04
Serverless 在大规模数据处理中的实践 2019-03-04
高可用Redis服务架构分析与搭建 2019-03-04
运营商的互联网蜕变,从沃云平台开始 2019-03-04
下一次 IT 变革:边缘计算(Edge computing) 2019-03-04
Gartner的预言:通向混合IT之旅 2019-03-04
Docker精华问答 | task与executor有什么关系? 2019-03-04
英特尔强势上新一大波数据产品,小伙伴们“奔走相告”…… | 极客头条 2019-03-04
成为最大的独立开源公司,对SUSE意味着什么? | 人物志 2019-03-04
Elastic全球用户大会Elastic{ON}首次落地北京 2019-03-04