在哪里可以找到可接受的pandas.timestamp格式的完整列表?

发布于 2025-02-12 08:24:58 字数 735 浏览 0 评论 0原文

我发现pandas.timestamp是非常强大且灵活的解析工具,可以接受各种时间戳/日期时间格式。例如,

In [38]: pd.Timestamp('2020')
Out[38]: Timestamp('2020-01-01 00:00:00')

In [39]: pd.Timestamp('2020-02')
Out[39]: Timestamp('2020-02-01 00:00:00')

In [40]: pd.Timestamp('2020Q1')
Out[40]: Timestamp('2020-01-01 00:00:00')

但它并不总是我期望的“魔术”,例如,以下是非法的:

In [41]: pd.Timestamp('202003')  # expecting 2020-03-01
ValueError: could not convert string to Timestamp

In [42]: pd.Timestamp('2020H2')  # expecting 2020-07-01, i.e. 2020 second half (start)
ValueError: could not convert string to Timestamp

我试图找到完整的支持格式列表,但看来文档丢失了(或者我缺少某些内容)。谁能帮忙?谢谢!

I find that pandas.Timestamp is extremely powerful and flexible parsing tool that accepts a wide range of timestamp/datetime formats. E.g.

In [38]: pd.Timestamp('2020')
Out[38]: Timestamp('2020-01-01 00:00:00')

In [39]: pd.Timestamp('2020-02')
Out[39]: Timestamp('2020-02-01 00:00:00')

In [40]: pd.Timestamp('2020Q1')
Out[40]: Timestamp('2020-01-01 00:00:00')

But it doesn't always do the "magic" I was expecting, e.g. the followings are illegal:

In [41]: pd.Timestamp('202003')  # expecting 2020-03-01
ValueError: could not convert string to Timestamp

In [42]: pd.Timestamp('2020H2')  # expecting 2020-07-01, i.e. 2020 second half (start)
ValueError: could not convert string to Timestamp

I tried to find a complete list of supported formats but it seems that the document is missing (or I'm missing something). Can anyone help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2025-02-19 08:24:58

如果研究年度

df=pd.DataFrame({'date':['2020-Q1']})
pd.PeriodIndex(df['date'], freq='Q').to_timestamp()

和日期,请使用四分之一

import pandas as pd
pd.to_datetime('202003', format='%Y%m')

Use quarters if looking into annual periods

df=pd.DataFrame({'date':['2020-Q1']})
pd.PeriodIndex(df['date'], freq='Q').to_timestamp()

and for dates

import pandas as pd
pd.to_datetime('202003', format='%Y%m')
━╋う一瞬間旳綻放 2025-02-19 08:24:58

pd.timestamp()是Python的DateTime等效的熊猫,在大多数情况下可以与之互换。 DateTime库接受ISO 8601日期格式。

在Python ISO 8601中,日期以yyyy-mm-ddthh:mm:ss.mmmmmm格式表示。例如,2022年5月18日,表示为2022-05-18T11:40:22.519222

  • yyyy:四位数格式的一年
  • mm:1-12 DD的月份:从1到31到31
  • t:这是分隔符字符那是在
    日期和时间字段。这是一个可选参数,默认
    “ t”的价值。
  • HH:对于分钟的值
  • mm:对于分钟的指定值
  • ss:对于秒的指定值
  • mmmmmmm :对于指定的微秒

source

直接从pandas文档(在这里 ):

基本上有三个针对构造函数的召集约定。主要形式接受四个参数。它们可以通过位置或关键字传递。

其他两种形式模仿了datetime.datetime的参数。他们
可以通过位置或关键字传递,但不会混合
一起。

示例

使用主要呼叫惯例:
这将转换一个类似日期的字符串的字符串,

>>> pd.Timestamp('2017-01-01T12')
Timestamp('2017-01-01 12:00:00')

的浮点

>>> pd.Timestamp(1513393355.5, unit='s')
Timestamp('2017-12-16 03:02:35.500000')

几秒钟的单位转换代表Unix epoch

>>> pd.Timestamp(1513393355, unit='s', tz='US/Pacific')
Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific')

以 :

>>> pd.Timestamp(2017, 1, 1, 12)
Timestamp('2017-01-01 12:00:00')

>>> pd.Timestamp(year=2017, month=1, day=1, hour=12)
Timestamp('2017-01-01 12:00:00')

pd.timestamp() is the pandas equivalent of python’s Datetime and is interchangeable with it in most cases. Datetime library accepts ISO 8601 date formats.

In Python ISO 8601 date is represented in YYYY-MM-DDTHH:MM:SS.mmmmmm format. For example, May 18, 2022, is represented as 2022-05-18T11:40:22.519222.

  • YYYY: Year in four-digit format
  • MM: Months from 1-12 DD: Days from 1 to 31
  • T: It is the separator character that is to be printed between the
    date and time fields. It is an optional parameter having a default
    value of “T”.
  • HH: For the value of minutes
  • MM: For the specified value of minutes
  • SS: For the specified value of seconds
  • mmmmmm: For the specified microseconds

Source

Directly from the Pandas documentation (here):

There are essentially three calling conventions for the constructor. The primary form accepts four parameters. They can be passed by position or keyword.

The other two forms mimic the parameters from datetime.datetime. They
can be passed by either position or keyword, but not both mixed
together.

Examples

Using the primary calling convention:
This converts a datetime-like string

>>> pd.Timestamp('2017-01-01T12')
Timestamp('2017-01-01 12:00:00')

This converts a float representing a Unix epoch in units of seconds

>>> pd.Timestamp(1513393355.5, unit='s')
Timestamp('2017-12-16 03:02:35.500000')

This converts an int representing a Unix-epoch in units of seconds and for a particular timezone

>>> pd.Timestamp(1513393355, unit='s', tz='US/Pacific')
Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific')

Using the other two forms that mimic the API for datetime.datetime:

>>> pd.Timestamp(2017, 1, 1, 12)
Timestamp('2017-01-01 12:00:00')

>>> pd.Timestamp(year=2017, month=1, day=1, hour=12)
Timestamp('2017-01-01 12:00:00')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文