Skip to content

Commit 18e0a26

Browse files
authored
Merge branch 'main' into test_date_range_for_bug_57456
2 parents dcb2ea2 + 66e465e commit 18e0a26

File tree

8 files changed

+222
-139
lines changed

8 files changed

+222
-139
lines changed

ci/code_checks.sh

Lines changed: 1 addition & 102 deletions
Large diffs are not rendered by default.

pandas/_libs/tslibs/offsets.pyx

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,42 @@ cdef class BaseOffset:
595595

596596
@property
597597
def rule_code(self) -> str:
598+
"""
599+
Return a string representing the base frequency.
600+
601+
See Also
602+
--------
603+
tseries.offsets.Hour.rule_code :
604+
Returns a string representing the base frequency of 'h'.
605+
tseries.offsets.Day.rule_code :
606+
Returns a string representing the base frequency of 'D'.
607+
608+
Examples
609+
--------
610+
>>> pd.offsets.Hour().rule_code
611+
'h'
612+
613+
>>> pd.offsets.Week(5).rule_code
614+
'W'
615+
"""
598616
return self._prefix
599617

600618
@cache_readonly
601619
def freqstr(self) -> str:
602620
"""
603621
Return a string representing the frequency.
604622

623+
See Also
624+
--------
625+
tseries.offsets.BusinessDay.freqstr :
626+
Return a string representing an offset frequency in Business Days.
627+
tseries.offsets.BusinessHour.freqstr :
628+
Return a string representing an offset frequency in Business Hours.
629+
tseries.offsets.Week.freqstr :
630+
Return a string representing an offset frequency in Weeks.
631+
tseries.offsets.Hour.freqstr :
632+
Return a string representing an offset frequency in Hours.
633+
605634
Examples
606635
--------
607636
>>> pd.DateOffset(5).freqstr
@@ -779,6 +808,26 @@ cdef class BaseOffset:
779808

780809
@property
781810
def nanos(self):
811+
"""
812+
Returns a integer of the total number of nanoseconds for fixed frequencies.
813+
814+
Raises
815+
------
816+
ValueError
817+
If the frequency is non-fixed.
818+
819+
See Also
820+
--------
821+
tseries.offsets.Hour.nanos :
822+
Returns an integer of the total number of nanoseconds.
823+
tseries.offsets.Day.nanos :
824+
Returns an integer of the total number of nanoseconds.
825+
826+
Examples
827+
--------
828+
>>> pd.offsets.Week(n=1).nanos
829+
ValueError: Week: weekday=None is a non-fixed frequency
830+
"""
782831
raise ValueError(f"{self} is a non-fixed frequency")
783832

784833
# ------------------------------------------------------------------
@@ -986,12 +1035,14 @@ cdef class Tick(SingleConstructorOffset):
9861035
@property
9871036
def nanos(self) -> int64_t:
9881037
"""
989-
Return an integer of the total number of nanoseconds.
1038+
Returns an integer of the total number of nanoseconds.
9901039

991-
Raises
992-
------
993-
ValueError
994-
If the frequency is non-fixed.
1040+
See Also
1041+
--------
1042+
tseries.offsets.Hour.nanos :
1043+
Returns an integer of the total number of nanoseconds.
1044+
tseries.offsets.Day.nanos :
1045+
Returns an integer of the total number of nanoseconds.
9951046

9961047
Examples
9971048
--------
@@ -2426,6 +2477,24 @@ cdef class WeekOfMonthMixin(SingleConstructorOffset):
24262477

24272478
@property
24282479
def rule_code(self) -> str:
2480+
"""
2481+
Return a string representing the base frequency.
2482+
2483+
See Also
2484+
--------
2485+
tseries.offsets.Hour.rule_code :
2486+
Returns a string representing the base frequency of 'h'.
2487+
tseries.offsets.Day.rule_code :
2488+
Returns a string representing the base frequency of 'D'.
2489+
2490+
Examples
2491+
--------
2492+
>>> pd.offsets.Week(5).rule_code
2493+
'W'
2494+
2495+
>>> pd.offsets.WeekOfMonth(n=1, week=0, weekday=0).rule_code
2496+
'WOM-1MON'
2497+
"""
24292498
weekday = int_to_weekday.get(self.weekday, "")
24302499
if self.week == -1:
24312500
# LastWeekOfMonth
@@ -2472,6 +2541,24 @@ cdef class YearOffset(SingleConstructorOffset):
24722541

24732542
@property
24742543
def rule_code(self) -> str:
2544+
"""
2545+
Return a string representing the base frequency.
2546+
2547+
See Also
2548+
--------
2549+
tseries.offsets.Hour.rule_code :
2550+
Returns a string representing the base frequency of 'h'.
2551+
tseries.offsets.Day.rule_code :
2552+
Returns a string representing the base frequency of 'D'.
2553+
2554+
Examples
2555+
--------
2556+
>>> pd.tseries.offsets.YearBegin(n=1, month=2).rule_code
2557+
'YS-FEB'
2558+
2559+
>>> pd.tseries.offsets.YearEnd(n=1, month=6).rule_code
2560+
'YE-JUN'
2561+
"""
24752562
month = MONTH_ALIASES[self.month]
24762563
return f"{self._prefix}-{month}"
24772564

@@ -3458,6 +3545,24 @@ cdef class Week(SingleConstructorOffset):
34583545

34593546
@property
34603547
def rule_code(self) -> str:
3548+
"""
3549+
Return a string representing the base frequency.
3550+
3551+
See Also
3552+
--------
3553+
tseries.offsets.Hour.name :
3554+
Returns a string representing the base frequency of 'h'.
3555+
tseries.offsets.Day.name :
3556+
Returns a string representing the base frequency of 'D'.
3557+
3558+
Examples
3559+
--------
3560+
>>> pd.offsets.Hour().rule_code
3561+
'h'
3562+
3563+
>>> pd.offsets.Week(5).rule_code
3564+
'W'
3565+
"""
34613566
suffix = ""
34623567
if self.weekday is not None:
34633568
weekday = int_to_weekday[self.weekday]

pandas/_libs/tslibs/period.pyx

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,20 +1913,58 @@ cdef class _Period(PeriodMixin):
19131913
Parameters
19141914
----------
19151915
freq : str, BaseOffset
1916-
The desired frequency. If passing a `str`, it needs to be a
1917-
valid :ref:`period alias <timeseries.period_aliases>`.
1916+
The target frequency to convert the Period object to.
1917+
If a string is provided,
1918+
it must be a valid :ref:`period alias <timeseries.period_aliases>`.
1919+
19181920
how : {'E', 'S', 'end', 'start'}, default 'end'
1919-
Start or end of the timespan.
1921+
Specifies whether to align the period to the start or end of the interval:
1922+
- 'E' or 'end': Align to the end of the interval.
1923+
- 'S' or 'start': Align to the start of the interval.
19201924

19211925
Returns
19221926
-------
1923-
resampled : Period
1927+
Period : Period object with the specified frequency, aligned to the parameter.
1928+
1929+
See Also
1930+
--------
1931+
Period.end_time : Return the end Timestamp.
1932+
Period.start_time : Return the start Timestamp.
1933+
Period.dayofyear : Return the day of the year.
1934+
Period.dayofweek : Return the day of the week.
19241935

19251936
Examples
19261937
--------
1927-
>>> period = pd.Period('2023-1-1', freq='D')
1938+
Convert a daily period to an hourly period, aligning to the end of the day:
1939+
1940+
>>> period = pd.Period('2023-01-01', freq='D')
19281941
>>> period.asfreq('h')
19291942
Period('2023-01-01 23:00', 'h')
1943+
1944+
Convert a monthly period to a daily period, aligning to the start of the month:
1945+
1946+
>>> period = pd.Period('2023-01', freq='M')
1947+
>>> period.asfreq('D', how='start')
1948+
Period('2023-01-01', 'D')
1949+
1950+
Convert a yearly period to a monthly period, aligning to the last month:
1951+
1952+
>>> period = pd.Period('2023', freq='Y')
1953+
>>> period.asfreq('M', how='end')
1954+
Period('2023-12', 'M')
1955+
1956+
Convert a monthly period to an hourly period,
1957+
aligning to the first day of the month:
1958+
1959+
>>> period = pd.Period('2023-01', freq='M')
1960+
>>> period.asfreq('h', how='start')
1961+
Period('2023-01-01 00:00', 'H')
1962+
1963+
Convert a weekly period to a daily period, aligning to the last day of the week:
1964+
1965+
>>> period = pd.Period('2023-08-01', freq='W')
1966+
>>> period.asfreq('D', how='end')
1967+
Period('2023-08-04', 'D')
19301968
"""
19311969
freq = self._maybe_convert_freq(freq)
19321970
how = validate_end_alias(how)
@@ -2014,11 +2052,45 @@ cdef class _Period(PeriodMixin):
20142052
"""
20152053
Return the month this Period falls on.
20162054

2055+
Returns
2056+
-------
2057+
int
2058+
2059+
See Also
2060+
--------
2061+
period.week : Get the week of the year on the given Period.
2062+
Period.year : Return the year this Period falls on.
2063+
Period.day : Return the day of the month this Period falls on.
2064+
2065+
Notes
2066+
-----
2067+
The month is based on the `ordinal` and `base` attributes of the Period.
2068+
20172069
Examples
20182070
--------
2071+
Create a Period object for January 2022 and get the month:
2072+
20192073
>>> period = pd.Period('2022-01', 'M')
20202074
>>> period.month
20212075
1
2076+
2077+
Period object with no specified frequency, resulting in a default frequency:
2078+
2079+
>>> period = pd.Period('2022', 'Y')
2080+
>>> period.month
2081+
12
2082+
2083+
Create a Period object with a specified frequency but an incomplete date string:
2084+
2085+
>>> period = pd.Period('2022', 'M')
2086+
>>> period.month
2087+
1
2088+
2089+
Handle a case where the Period object is empty, which results in `NaN`:
2090+
2091+
>>> period = pd.Period('nan', 'M')
2092+
>>> period.month
2093+
nan
20222094
"""
20232095
base = self._dtype._dtype_code
20242096
return pmonth(self.ordinal, base)

pandas/core/arrays/numpy_.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,3 @@ def _wrap_ndarray_result(self, result: np.ndarray):
557557

558558
return TimedeltaArray._simple_new(result, dtype=result.dtype)
559559
return type(self)(result)
560-
561-
# ------------------------------------------------------------------------
562-
# String methods interface
563-
_str_na_value = np.nan

pandas/core/arrays/string_.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,6 @@ def _cmp_method(self, other, op):
848848

849849
_arith_method = _cmp_method
850850

851-
# ------------------------------------------------------------------------
852-
# String methods interface
853-
# error: Incompatible types in assignment (expression has type "NAType",
854-
# base class "NumpyExtensionArray" defined the type as "float")
855-
_str_na_value = libmissing.NA # type: ignore[assignment]
856-
857851

858852
class StringArrayNumpySemantics(StringArray):
859853
_storage = "python"
@@ -884,7 +878,3 @@ def _from_backing_data(self, arr: np.ndarray) -> StringArrayNumpySemantics:
884878
# need to override NumpyExtensionArray._from_backing_data to ensure
885879
# we always preserve the dtype
886880
return NDArrayBacked._from_backing_data(self, arr)
887-
888-
# ------------------------------------------------------------------------
889-
# String methods interface
890-
_str_na_value = np.nan

pandas/core/arrays/string_arrow.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ def astype(self, dtype, copy: bool = True):
275275
# ------------------------------------------------------------------------
276276
# String methods interface
277277

278-
# error: Incompatible types in assignment (expression has type "NAType",
279-
# base class "ObjectStringArrayMixin" defined the type as "float")
280-
_str_na_value = libmissing.NA # type: ignore[assignment]
281-
282278
_str_map = BaseStringArray._str_map
283279

284280
def _str_contains(

pandas/core/indexes/multi.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,17 @@ def _set_names(self, names, *, level=None, validate: bool = True) -> None:
16361636
doc="""
16371637
Names of levels in MultiIndex.
16381638
1639+
This attribute provides access to the names of the levels in a `MultiIndex`.
1640+
The names are stored as a `FrozenList`, which is an immutable list-like
1641+
container. Each name corresponds to a level in the `MultiIndex`, and can be
1642+
used to identify or manipulate the levels individually.
1643+
1644+
See Also
1645+
--------
1646+
MultiIndex.set_names : Set Index or MultiIndex name.
1647+
MultiIndex.rename : Rename specific levels in a MultiIndex.
1648+
Index.names : Get names on index.
1649+
16391650
Examples
16401651
--------
16411652
>>> mi = pd.MultiIndex.from_arrays(
@@ -2681,8 +2692,15 @@ def sortlevel(
26812692
"""
26822693
Sort MultiIndex at the requested level.
26832694
2684-
The result will respect the original ordering of the associated
2685-
factor at that level.
2695+
This method is useful when dealing with MultiIndex objects, allowing for
2696+
sorting at a specific level of the index. The function preserves the
2697+
relative ordering of data within the same level while sorting
2698+
the overall MultiIndex. The method provides flexibility with the `ascending`
2699+
parameter to define the sort order and with the `sort_remaining` parameter to
2700+
control whether the remaining levels should also be sorted. Sorting a
2701+
MultiIndex can be crucial when performing operations that require ordered
2702+
indices, such as grouping or merging datasets. The `na_position` argument is
2703+
important in handling missing values consistently across different levels.
26862704
26872705
Parameters
26882706
----------
@@ -2692,7 +2710,9 @@ def sortlevel(
26922710
ascending : bool, default True
26932711
False to sort in descending order.
26942712
Can also be a list to specify a directed ordering.
2695-
sort_remaining : sort by the remaining levels after level
2713+
sort_remaining : bool, default True
2714+
If True, sorts by the remaining levels after sorting by the specified
2715+
`level`.
26962716
na_position : {'first' or 'last'}, default 'first'
26972717
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
26982718
the end.
@@ -2706,6 +2726,13 @@ def sortlevel(
27062726
indexer : np.ndarray[np.intp]
27072727
Indices of output values in original index.
27082728
2729+
See Also
2730+
--------
2731+
MultiIndex : A multi-level, or hierarchical, index object for pandas objects.
2732+
Index.sort_values : Sort Index values.
2733+
DataFrame.sort_index : Sort DataFrame by the index.
2734+
Series.sort_index : Sort Series by the index.
2735+
27092736
Examples
27102737
--------
27112738
>>> mi = pd.MultiIndex.from_arrays([[0, 0], [2, 1]])

0 commit comments

Comments
 (0)