需要周日作为第一天

2024-05-19 03:02:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望Sunday为0,但当使用datetime weeday方法时,它是6:

datetime(2013, 6, 9, 11, 59, 59, 1).weekday()  # this returns 6

我的地方是:“恩_美国UTF-8“所以Sunday应该为0(当我在bash提示符下运行'locale day'时,它正确地将'Sunday'显示为一周的第一天)。在

^{pr2}$

如何让python将星期天显示为一周的第一天?在

(我使用pandas timeseries并调用.date来获取日期时间。)


Tags: 方法bashpandasdatetime地方thislocaleutf
2条回答

使用DateOfWeek属性比通过dayofweek创建对象更快:

In [1]: rng = pd.date_range('1/1/2013', periods=72, freq='D')

In [2]: (rng.dayofweek + 1 ) % 7
Out[2]:
array([2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3,
       4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5,
       6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0,
       1, 2, 3], dtype=int32)

可以将此属性设为属性:

^{pr2}$

同样,你可以计算你的周数:

In [4]: rng.my_week = rng.map(lambda x: x.week if x.dayofweek else x.week - 1)

然后作为属性提供给您。在

一。在

在日期时间.日期weekday方法不是特定于语言环境的,它是硬编码的(这里是它的docstring):

Return the day of the week represented by the date.
Monday == 0 ... Sunday == 6

试试这个:

(datetime.datetime(2013, 6, 9, 11, 59, 59, 1).weekday() + 1) % 7

相关问题 更多 >

    热门问题