我想知道为什么
>>> pandas.Timestamp(dt.datetime(2009,1,6)).week
和
>>> datetime.datetime(2009,1,6).isocalendar()[1]
不要给出相同的结果。
First -- you need to get out of the habit of starting integers with 0
s. 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.