Skip to content

Commit 592793c

Browse files
committed
add to to_offset a boolean argument is_period
1 parent 33236ef commit 592793c

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

pandas/_libs/tslibs/offsets.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from numpy cimport int64_t
22

33

4-
cpdef to_offset(object obj)
4+
cpdef to_offset(object obj, is_period=*)
55
cdef bint is_offset_object(object obj)
66
cdef bint is_tick_object(object obj)
77

pandas/_libs/tslibs/offsets.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ class SingleConstructorOffset(BaseOffset):
101101
def __reduce__(self): ...
102102

103103
@overload
104-
def to_offset(freq: None) -> None: ...
104+
def to_offset(freq: None, is_period: bool) -> None: ...
105105
@overload
106-
def to_offset(freq: _BaseOffsetT) -> _BaseOffsetT: ...
106+
def to_offset(freq: _BaseOffsetT, is_period: bool) -> _BaseOffsetT: ...
107107
@overload
108-
def to_offset(freq: timedelta | str) -> BaseOffset: ...
108+
def to_offset(freq: timedelta | str, is_period: bool) -> BaseOffset: ...
109109

110110
class Tick(SingleConstructorOffset):
111111
_creso: int

pandas/_libs/tslibs/offsets.pyx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,12 @@ cdef class MonthOffset(SingleConstructorOffset):
25422542
BaseOffset.__setstate__(self, state)
25432543

25442544

2545+
cdef class Month(MonthOffset):
2546+
_period_dtype_code = PeriodDtypeCode.M
2547+
_prefix = "M"
2548+
_day_opt = "end"
2549+
2550+
25452551
cdef class MonthEnd(MonthOffset):
25462552
"""
25472553
DateOffset of one month end.
@@ -4070,6 +4076,7 @@ prefix_mapping = {
40704076
CustomBusinessMonthEnd, # 'CBM'
40714077
CustomBusinessMonthBegin, # 'CBMS'
40724078
CustomBusinessHour, # 'CBH'
4079+
Month, # 'M'
40734080
MonthEnd, # 'ME'
40744081
MonthBegin, # 'MS'
40754082
Nano, # 'N'
@@ -4156,7 +4163,7 @@ def _get_offset(name: str) -> BaseOffset:
41564163
return _offset_map[name]
41574164

41584165

4159-
cpdef to_offset(freq):
4166+
cpdef to_offset(freq, is_period=False):
41604167
"""
41614168
Return DateOffset object from string or datetime.timedelta object.
41624169
@@ -4224,7 +4231,7 @@ cpdef to_offset(freq):
42244231

42254232
tups = zip(split[0::4], split[1::4], split[2::4])
42264233
for n, (sep, stride, name) in enumerate(tups):
4227-
if name == "M":
4234+
if name == "M" and not is_period:
42284235
warnings.warn(
42294236
r"\'M\' will be deprecated, please use \'ME\' "
42304237
"for \'month end\'",

pandas/core/arrays/period.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,14 @@ def _scalar_type(self) -> type[Period]:
208208
# Constructors
209209

210210
def __init__(
211-
self, values, dtype: Dtype | None = None, freq=None, copy: bool = False
211+
self,
212+
values,
213+
dtype: Dtype | None = None,
214+
freq=None,
215+
copy: bool = False,
216+
is_period: bool = True,
212217
) -> None:
213-
freq = validate_dtype_freq(dtype, freq)
218+
freq = validate_dtype_freq(dtype, freq, is_period)
214219

215220
if freq is not None:
216221
freq = Period._maybe_convert_freq(freq)
@@ -260,7 +265,7 @@ def _from_sequence(
260265
freq = None
261266

262267
if isinstance(scalars, cls):
263-
validate_dtype_freq(scalars.dtype, freq)
268+
validate_dtype_freq(scalars.dtype, freq, True)
264269
if copy:
265270
scalars = scalars.copy()
266271
return scalars
@@ -938,7 +943,7 @@ def validate_dtype_freq(dtype, freq: timedelta | str | None) -> BaseOffset:
938943

939944

940945
def validate_dtype_freq(
941-
dtype, freq: BaseOffsetT | timedelta | str | None
946+
dtype, freq: BaseOffsetT | timedelta | str | None, is_period: bool | False
942947
) -> BaseOffsetT:
943948
"""
944949
If both a dtype and a freq are available, ensure they match. If only
@@ -962,7 +967,7 @@ def validate_dtype_freq(
962967
# error: Incompatible types in assignment (expression has type
963968
# "BaseOffset", variable has type "Union[BaseOffsetT, timedelta,
964969
# str, None]")
965-
freq = to_offset(freq) # type: ignore[assignment]
970+
freq = to_offset(freq, is_period) # type: ignore[assignment]
966971

967972
if dtype is not None:
968973
dtype = pandas_dtype(dtype)

pandas/core/indexes/period.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class PeriodIndex(DatetimeIndexOpsMixin):
152152
_data: PeriodArray
153153
freq: BaseOffset
154154
dtype: PeriodDtype
155+
is_period: bool
155156

156157
_data_cls = PeriodArray
157158
_supports_partial_string_indexing = True
@@ -210,6 +211,7 @@ def __new__(
210211
dtype: Dtype | None = None,
211212
copy: bool = False,
212213
name: Hashable = None,
214+
is_period: bool = True,
213215
**fields,
214216
) -> PeriodIndex:
215217
valid_field_set = {
@@ -245,7 +247,7 @@ def __new__(
245247

246248
data = PeriodArray(data, freq=freq)
247249
else:
248-
freq = validate_dtype_freq(dtype, freq)
250+
freq = validate_dtype_freq(dtype, freq, is_period)
249251

250252
# PeriodIndex allow PeriodIndex(period_index, freq=different)
251253
# Let's not encourage that kind of behavior in PeriodArray.

0 commit comments

Comments
 (0)