1

我想知道为什么

>>> pandas.Timestamp(dt.datetime(2009,1,6)).week

>>> datetime.datetime(2009,1,6).isocalendar()[1]

不要给出相同的结果。

4

1 回答 1

6

First -- you need to get out of the habit of starting integers with 0s. This means they'll be interpreted as octal (base-8) constants, which leads to confusion:

>>> 10
10
>>> 010
8

As for why they give two different answers, there are two reasons:

(1) You're not comparing the week numbers. As the documentation says, isocalendar "[r]eturn[s] a 3-tuple containing ISO year, week number, and weekday." So isocalendar()[2] will give you the ISO weekday, not the week.

(2) ISO dates are defined a little differently than you might expect (explained here). For example, in the most severe case:

>>> dt.datetime(2010,1,1).isocalendar()
(2009, 53, 5)
>>> pd.Timestamp(dt.datetime(2010,1,1)).week
1

so there will often be a difference.

于 2012-09-30T15:43:03.507 回答