Skip to content

Commit 64a48de

Browse files
jbrockmendeldavid-liu-brattle-1
authored andcommitted
use ccalendar.get_days_in_month, deprecate tslib.monthrange (pandas-dev#21451)
1 parent ce68870 commit 64a48de

File tree

6 files changed

+9
-27
lines changed

6 files changed

+9
-27
lines changed

pandas/_libs/tslib.pyx

+1-15
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ from tslibs.np_datetime cimport (check_dts_bounds,
2525
_string_to_dts,
2626
dt64_to_dtstruct, dtstruct_to_dt64,
2727
pydatetime_to_dt64, pydate_to_dt64,
28-
get_datetime64_value,
29-
days_per_month_table,
30-
dayofweek, is_leapyear)
28+
get_datetime64_value)
3129
from tslibs.np_datetime import OutOfBoundsDatetime
3230

3331
from tslibs.parsing import parse_datetime_string
@@ -763,18 +761,6 @@ cdef inline bint _parse_today_now(str val, int64_t* iresult):
763761
# Some general helper functions
764762

765763

766-
def monthrange(int64_t year, int64_t month):
767-
cdef:
768-
int64_t days
769-
770-
if month < 1 or month > 12:
771-
raise ValueError("bad month number 0; must be 1-12")
772-
773-
days = days_per_month_table[is_leapyear(year)][month - 1]
774-
775-
return (dayofweek(year, month, 1), days)
776-
777-
778764
cpdef normalize_date(object dt):
779765
"""
780766
Normalize datetime.datetime value to midnight. Returns datetime.date as a

pandas/_libs/tslibs/resolution.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from fields import build_field_sarray
2525
from conversion import tz_convert
2626
from conversion cimport tz_convert_utc_to_tzlocal
2727
from ccalendar import MONTH_ALIASES, int_to_weekday
28+
from ccalendar cimport get_days_in_month
2829

2930
from pandas._libs.properties import cache_readonly
3031
from pandas._libs.tslib import Timestamp
@@ -487,7 +488,6 @@ class _FrequencyInferer(object):
487488
days = self.fields['D']
488489
weekdays = self.index.dayofweek
489490

490-
from calendar import monthrange
491491
for y, m, d, wd in zip(years, months, days, weekdays):
492492

493493
if calendar_start:
@@ -496,7 +496,7 @@ class _FrequencyInferer(object):
496496
business_start &= d == 1 or (d <= 3 and wd == 0)
497497

498498
if calendar_end or business_end:
499-
_, daysinmonth = monthrange(y, m)
499+
daysinmonth = get_days_in_month(y, m)
500500
cal = d == daysinmonth
501501
if calendar_end:
502502
calendar_end &= cal

pandas/core/indexes/datetimes.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from pandas._libs import (lib, index as libindex, tslib as libts,
5656
join as libjoin, Timestamp)
5757
from pandas._libs.tslibs import (timezones, conversion, fields, parsing,
58+
ccalendar,
5859
resolution as libresolution)
5960

6061
# -------- some conversion wrapper functions
@@ -1451,14 +1452,14 @@ def _parsed_string_to_bounds(self, reso, parsed):
14511452
Timestamp(datetime(parsed.year, 12, 31, 23,
14521453
59, 59, 999999), tz=self.tz))
14531454
elif reso == 'month':
1454-
d = libts.monthrange(parsed.year, parsed.month)[1]
1455+
d = ccalendar.get_days_in_month(parsed.year, parsed.month)
14551456
return (Timestamp(datetime(parsed.year, parsed.month, 1),
14561457
tz=self.tz),
14571458
Timestamp(datetime(parsed.year, parsed.month, d, 23,
14581459
59, 59, 999999), tz=self.tz))
14591460
elif reso == 'quarter':
14601461
qe = (((parsed.month - 1) + 2) % 12) + 1 # two months ahead
1461-
d = libts.monthrange(parsed.year, qe)[1] # at end of month
1462+
d = ccalendar.get_days_in_month(parsed.year, qe) # at end of month
14621463
return (Timestamp(datetime(parsed.year, parsed.month, 1),
14631464
tz=self.tz),
14641465
Timestamp(datetime(parsed.year, qe, d, 23, 59,

pandas/tests/tseries/offsets/test_offsets.py

-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@
4141
from .common import assert_offset_equal, assert_onOffset
4242

4343

44-
def test_monthrange():
45-
import calendar
46-
for y in range(2000, 2013):
47-
for m in range(1, 13):
48-
assert tslib.monthrange(y, m) == calendar.monthrange(y, m)
49-
5044
####
5145
# Misc function tests
5246
####

pandas/tseries/offsets.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ def apply(self, other):
11401140
# shift `other` to self.day_of_month, incrementing `n` if necessary
11411141
n = liboffsets.roll_convention(other.day, self.n, self.day_of_month)
11421142

1143-
days_in_month = tslib.monthrange(other.year, other.month)[1]
1143+
days_in_month = ccalendar.get_days_in_month(other.year, other.month)
11441144

11451145
# For SemiMonthBegin on other.day == 1 and
11461146
# SemiMonthEnd on other.day == days_in_month,
@@ -1217,7 +1217,7 @@ class SemiMonthEnd(SemiMonthOffset):
12171217
def onOffset(self, dt):
12181218
if self.normalize and not _is_normalized(dt):
12191219
return False
1220-
_, days_in_month = tslib.monthrange(dt.year, dt.month)
1220+
days_in_month = ccalendar.get_days_in_month(dt.year, dt.month)
12211221
return dt.day in (self.day_of_month, days_in_month)
12221222

12231223
def _apply(self, n, other):

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def pxd(name):
603603
'pyxfile': '_libs/tslibs/resolution',
604604
'pxdfiles': ['_libs/src/util',
605605
'_libs/khash',
606+
'_libs/tslibs/ccalendar',
606607
'_libs/tslibs/frequencies',
607608
'_libs/tslibs/timezones'],
608609
'depends': tseries_depends,

0 commit comments

Comments
 (0)