Skip to content

Commit 5415e74

Browse files
authored
Merge branch 'master' into reindex_tzaware
2 parents 933cec6 + cc1c406 commit 5415e74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+658
-528
lines changed

doc/source/whatsnew/v1.0.1.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ including other versions of pandas.
1515

1616
Bug fixes
1717
~~~~~~~~~
18-
18+
- Bug in :meth:`GroupBy.apply` was raising ``TypeError`` if called with function which returned a non-pandas non-scalar object (e.g. a list) (:issue:`31441`)
1919

2020
Categorical
2121
^^^^^^^^^^^
@@ -25,8 +25,8 @@ Categorical
2525

2626
Datetimelike
2727
^^^^^^^^^^^^
28-
-
29-
-
28+
- Fixed regression in :meth:`to_datetime` when parsing non-nanosecond resolution datetimes (:issue:`31491`)
29+
- Fixed bug in :meth:`to_datetime` raising when ``cache=True`` and out-of-bound values are present (:issue:`31491`)
3030

3131
Timedelta
3232
^^^^^^^^^
@@ -44,6 +44,7 @@ Timezones
4444
Numeric
4545
^^^^^^^
4646
- Bug in dtypes being lost in ``DataFrame.__invert__`` (``~`` operator) with mixed dtypes (:issue:`31183`)
47+
- Bug in :class:`Series` multiplication when multiplying a numeric :class:`Series` with >10000 elements with a timedelta-like scalar (:issue:`31467`)
4748
-
4849

4950
Conversion
@@ -68,8 +69,11 @@ Interval
6869
Indexing
6970
^^^^^^^^
7071

72+
- Fixed regression when indexing a ``Series`` or ``DataFrame`` indexed by ``DatetimeIndex`` with a slice containg a :class:`datetime.date` (:issue:`31501`)
73+
- Fixed regression in :class:`DataFrame` setting values with a slice (e.g. ``df[-4:] = 1``) indexing by label instead of position (:issue:`31469`)
7174
-
7275
-
76+
- Bug where assigning to a :class:`Series` using a IntegerArray / BooleanArray as a mask would raise ``TypeError`` (:issue:`31446`)
7377

7478
Missing
7579
^^^^^^^
@@ -86,6 +90,7 @@ MultiIndex
8690
I/O
8791
^^^
8892

93+
- Fixed regression in :meth:`~DataFrame.to_csv` where specifying an ``na_rep`` might truncate the values written (:issue:`31447`)
8994
-
9095
-
9196

doc/source/whatsnew/v1.1.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ Indexing
150150
- Bug in :meth:`PeriodIndex.get_loc` treating higher-resolution strings differently from :meth:`PeriodIndex.get_value` (:issue:`31172`)
151151
- Bug in :meth:`Series.at` and :meth:`DataFrame.at` not matching ``.loc`` behavior when looking up an integer in a :class:`Float64Index` (:issue:`31329`)
152152
- Bug in :meth:`PeriodIndex.is_monotonic` incorrectly returning ``True`` when containing leading ``NaT`` entries (:issue:`31437`)
153-
- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`)
153+
- Bug in :meth:`DatetimeIndex.get_loc` raising ``KeyError`` with converted-integer key instead of the user-passed key (:issue:`31425`)
154+
- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index and method='nearest' (:issue:`26683`)
154155

155156
Missing
156157
^^^^^^^

pandas/_libs/reduction.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ def apply_frame_axis0(object frame, object f, object names,
501501

502502
if not is_scalar(piece):
503503
# Need to copy data to avoid appending references
504-
if hasattr(piece, "copy"):
504+
try:
505505
piece = piece.copy(deep="all")
506-
else:
506+
except (TypeError, AttributeError):
507507
piece = copy(piece)
508508

509509
results.append(piece)

pandas/_libs/tslibs/period.pyx

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,6 @@ cdef int DtoQ_yq(int64_t ordinal, asfreq_info *af_info, int *year) nogil:
473473
int quarter
474474

475475
pandas_datetime_to_datetimestruct(ordinal, NPY_FR_D, &dts)
476-
# TODO: Another version of this function used
477-
# date_info_from_days_and_time(&dts, unix_date, 0)
478-
# instead of pandas_datetime_to_datetimestruct; is one more performant?
479476
if af_info.to_end != 12:
480477
dts.month -= af_info.to_end
481478
if dts.month <= 0:
@@ -516,7 +513,7 @@ cdef int64_t asfreq_DTtoW(int64_t ordinal, asfreq_info *af_info) nogil:
516513
# Conversion _from_ BusinessDay Freq
517514

518515
cdef int64_t asfreq_BtoDT(int64_t ordinal, asfreq_info *af_info) nogil:
519-
ordinal = ((ordinal + 3) // 5) * 7 + (ordinal + 3) % 5 -3
516+
ordinal = ((ordinal + 3) // 5) * 7 + (ordinal + 3) % 5 - 3
520517
return upsample_daytime(ordinal, af_info)
521518

522519

@@ -753,14 +750,7 @@ cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil:
753750
if fmonth == 0:
754751
fmonth = 12
755752

756-
mdiff = dts.month - fmonth
757-
# TODO: Aren't the next two conditions equivalent to
758-
# unconditional incrementing?
759-
if mdiff < 0:
760-
mdiff += 12
761-
if dts.month >= fmonth:
762-
mdiff += 12
763-
753+
mdiff = dts.month - fmonth + 12
764754
return (dts.year - 1970) * 4 + (mdiff - 1) // 3
765755

766756
elif freq == FR_MTH:
@@ -804,17 +794,16 @@ cdef int64_t get_period_ordinal(npy_datetimestruct *dts, int freq) nogil:
804794
delta = (unix_date + 3) % 7 + 1
805795
# return the number of business days in full weeks plus the business
806796
# days in the last - possible partial - week
807-
if delta <= 5:
808-
return (5 * weeks) + delta - 4
809-
else:
810-
return (5 * weeks) + (5 + 1) - 4
797+
if delta > 6:
798+
# We have a Sunday, which rolls back to the previous Friday,
799+
# just like Saturday, so decrement delta by 1 to treat as saturday
800+
delta = 6
801+
return (5 * weeks) + delta - 4
811802

812803
elif freq_group == FR_WK:
813804
day_adj = freq - FR_WK
814805
return (unix_date + 3 - day_adj) // 7 + 1
815806

816-
# raise ValueError
817-
818807

819808
cdef void get_date_info(int64_t ordinal, int freq,
820809
npy_datetimestruct *dts) nogil:
@@ -983,7 +972,7 @@ cdef inline int month_to_quarter(int month) nogil:
983972

984973
@cython.wraparound(False)
985974
@cython.boundscheck(False)
986-
def dt64arr_to_periodarr(int64_t[:] dtarr, int freq, tz=None):
975+
def dt64arr_to_periodarr(const int64_t[:] dtarr, int freq, tz=None):
987976
"""
988977
Convert array of datetime64 values (passed in as 'i8' dtype) to a set of
989978
periods corresponding to desired frequency, per period convention.
@@ -1383,7 +1372,7 @@ cdef int pdays_in_month(int64_t ordinal, int freq):
13831372

13841373
@cython.wraparound(False)
13851374
@cython.boundscheck(False)
1386-
def get_period_field_arr(int code, int64_t[:] arr, int freq):
1375+
def get_period_field_arr(int code, const int64_t[:] arr, int freq):
13871376
cdef:
13881377
Py_ssize_t i, sz
13891378
int64_t[:] out
@@ -1496,7 +1485,7 @@ def extract_freq(ndarray[object] values):
14961485

14971486
@cython.wraparound(False)
14981487
@cython.boundscheck(False)
1499-
cdef int64_t[:] localize_dt64arr_to_period(int64_t[:] stamps,
1488+
cdef int64_t[:] localize_dt64arr_to_period(const int64_t[:] stamps,
15001489
int freq, object tz):
15011490
cdef:
15021491
Py_ssize_t n = len(stamps)
@@ -1584,7 +1573,7 @@ cdef class _Period:
15841573
return freq
15851574

15861575
@classmethod
1587-
def _from_ordinal(cls, ordinal, freq):
1576+
def _from_ordinal(cls, ordinal: int, freq) -> "Period":
15881577
"""
15891578
Fast creation from an ordinal and freq that are already validated!
15901579
"""
@@ -1704,7 +1693,7 @@ cdef class _Period:
17041693
else:
17051694
return NotImplemented
17061695

1707-
def asfreq(self, freq, how='E'):
1696+
def asfreq(self, freq, how='E') -> "Period":
17081697
"""
17091698
Convert Period to desired frequency, at the start or end of the interval.
17101699

@@ -1735,7 +1724,7 @@ cdef class _Period:
17351724
return Period(ordinal=ordinal, freq=freq)
17361725

17371726
@property
1738-
def start_time(self):
1727+
def start_time(self) -> Timestamp:
17391728
"""
17401729
Get the Timestamp for the start of the period.
17411730

@@ -1765,13 +1754,13 @@ cdef class _Period:
17651754
return self.to_timestamp(how='S')
17661755

17671756
@property
1768-
def end_time(self):
1757+
def end_time(self) -> Timestamp:
17691758
# freq.n can't be negative or 0
17701759
# ordinal = (self + self.freq.n).start_time.value - 1
17711760
ordinal = (self + self.freq).start_time.value - 1
17721761
return Timestamp(ordinal)
17731762

1774-
def to_timestamp(self, freq=None, how='start', tz=None):
1763+
def to_timestamp(self, freq=None, how='start', tz=None) -> Timestamp:
17751764
"""
17761765
Return the Timestamp representation of the Period.
17771766

@@ -1811,17 +1800,17 @@ cdef class _Period:
18111800
return Timestamp(dt64, tz=tz)
18121801

18131802
@property
1814-
def year(self):
1803+
def year(self) -> int:
18151804
base, mult = get_freq_code(self.freq)
18161805
return pyear(self.ordinal, base)
18171806

18181807
@property
1819-
def month(self):
1808+
def month(self) -> int:
18201809
base, mult = get_freq_code(self.freq)
18211810
return pmonth(self.ordinal, base)
18221811

18231812
@property
1824-
def day(self):
1813+
def day(self) -> int:
18251814
"""
18261815
Get day of the month that a Period falls on.
18271816

@@ -1844,7 +1833,7 @@ cdef class _Period:
18441833
return pday(self.ordinal, base)
18451834

18461835
@property
1847-
def hour(self):
1836+
def hour(self) -> int:
18481837
"""
18491838
Get the hour of the day component of the Period.
18501839

@@ -1874,7 +1863,7 @@ cdef class _Period:
18741863
return phour(self.ordinal, base)
18751864

18761865
@property
1877-
def minute(self):
1866+
def minute(self) -> int:
18781867
"""
18791868
Get minute of the hour component of the Period.
18801869

@@ -1898,7 +1887,7 @@ cdef class _Period:
18981887
return pminute(self.ordinal, base)
18991888

19001889
@property
1901-
def second(self):
1890+
def second(self) -> int:
19021891
"""
19031892
Get the second component of the Period.
19041893

@@ -1922,12 +1911,12 @@ cdef class _Period:
19221911
return psecond(self.ordinal, base)
19231912

19241913
@property
1925-
def weekofyear(self):
1914+
def weekofyear(self) -> int:
19261915
base, mult = get_freq_code(self.freq)
19271916
return pweek(self.ordinal, base)
19281917

19291918
@property
1930-
def week(self):
1919+
def week(self) -> int:
19311920
"""
19321921
Get the week of the year on the given Period.
19331922

@@ -1957,7 +1946,7 @@ cdef class _Period:
19571946
return self.weekofyear
19581947

19591948
@property
1960-
def dayofweek(self):
1949+
def dayofweek(self) -> int:
19611950
"""
19621951
Day of the week the period lies in, with Monday=0 and Sunday=6.
19631952

@@ -2008,7 +1997,7 @@ cdef class _Period:
20081997
return pweekday(self.ordinal, base)
20091998

20101999
@property
2011-
def weekday(self):
2000+
def weekday(self) -> int:
20122001
"""
20132002
Day of the week the period lies in, with Monday=0 and Sunday=6.
20142003

@@ -2061,7 +2050,7 @@ cdef class _Period:
20612050
return self.dayofweek
20622051

20632052
@property
2064-
def dayofyear(self):
2053+
def dayofyear(self) -> int:
20652054
"""
20662055
Return the day of the year.
20672056

@@ -2096,12 +2085,12 @@ cdef class _Period:
20962085
return pday_of_year(self.ordinal, base)
20972086

20982087
@property
2099-
def quarter(self):
2088+
def quarter(self) -> int:
21002089
base, mult = get_freq_code(self.freq)
21012090
return pquarter(self.ordinal, base)
21022091

21032092
@property
2104-
def qyear(self):
2093+
def qyear(self) -> int:
21052094
"""
21062095
Fiscal year the Period lies in according to its starting-quarter.
21072096

@@ -2145,7 +2134,7 @@ cdef class _Period:
21452134
return pqyear(self.ordinal, base)
21462135

21472136
@property
2148-
def days_in_month(self):
2137+
def days_in_month(self) -> int:
21492138
"""
21502139
Get the total number of days in the month that this period falls on.
21512140

@@ -2179,7 +2168,7 @@ cdef class _Period:
21792168
return pdays_in_month(self.ordinal, base)
21802169

21812170
@property
2182-
def daysinmonth(self):
2171+
def daysinmonth(self) -> int:
21832172
"""
21842173
Get the total number of days of the month that the Period falls in.
21852174

@@ -2209,7 +2198,7 @@ cdef class _Period:
22092198
return Period(datetime.now(), freq=freq)
22102199

22112200
@property
2212-
def freqstr(self):
2201+
def freqstr(self) -> str:
22132202
return self.freq.freqstr
22142203

22152204
def __repr__(self) -> str:

pandas/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,16 @@ def tick_classes(request):
868868
)
869869

870870

871+
@pytest.fixture
872+
def datetime_series():
873+
"""
874+
Fixture for Series of floats with DatetimeIndex
875+
"""
876+
s = tm.makeTimeSeries()
877+
s.name = "ts"
878+
return s
879+
880+
871881
@pytest.fixture
872882
def float_frame():
873883
"""

pandas/core/algorithms.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
is_categorical_dtype,
3030
is_complex_dtype,
3131
is_datetime64_any_dtype,
32+
is_datetime64_dtype,
3233
is_datetime64_ns_dtype,
3334
is_extension_array_dtype,
3435
is_float_dtype,
@@ -191,6 +192,11 @@ def _reconstruct_data(values, dtype, original):
191192
if isinstance(original, ABCIndexClass):
192193
values = values.astype(object, copy=False)
193194
elif dtype is not None:
195+
if is_datetime64_dtype(dtype):
196+
dtype = "datetime64[ns]"
197+
elif is_timedelta64_dtype(dtype):
198+
dtype = "timedelta64[ns]"
199+
194200
values = values.astype(dtype, copy=False)
195201

196202
return values

pandas/core/arrays/boolean.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pandas.core.dtypes.missing import isna, notna
2727

2828
from pandas.core import nanops, ops
29+
from pandas.core.indexers import check_array_indexer
2930

3031
from .masked import BaseMaskedArray
3132

@@ -382,6 +383,7 @@ def __setitem__(self, key, value):
382383
value = value[0]
383384
mask = mask[0]
384385

386+
key = check_array_indexer(self, key)
385387
self._data[key] = value
386388
self._mask[key] = mask
387389

pandas/core/arrays/categorical.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,8 @@ def __setitem__(self, key, value):
20732073

20742074
lindexer = self.categories.get_indexer(rvalue)
20752075
lindexer = self._maybe_coerce_indexer(lindexer)
2076+
2077+
key = check_array_indexer(self, key)
20762078
self._codes[key] = lindexer
20772079

20782080
def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:

0 commit comments

Comments
 (0)