Skip to content

Commit 510369f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into fix_convert_dtypes_bool
Merge remote-tracking branch 'upstream/master' into fix_convert_dtypes_bool
2 parents 06fcb7f + f6b3e82 commit 510369f

35 files changed

+656
-691
lines changed

doc/source/whatsnew/v1.1.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ Numeric
234234
Conversion
235235
^^^^^^^^^^
236236
- Bug in :class:`Series` construction from NumPy array with big-endian ``datetime64`` dtype (:issue:`29684`)
237-
-
237+
- Bug in :class:`Timedelta` construction with large nanoseconds keyword value (:issue:`32402`)
238238
-
239239

240240
Strings
@@ -327,6 +327,7 @@ Reshaping
327327
- Bug in :func:`crosstab` when inputs are two Series and have tuple names, the output will keep dummy MultiIndex as columns. (:issue:`18321`)
328328
- :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`)
329329
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
330+
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
330331
- :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`)
331332

332333

@@ -349,7 +350,6 @@ Other
349350
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
350351
- Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`)
351352
- Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`)
352-
-
353353

354354
.. ---------------------------------------------------------------------------
355355

pandas/_libs/tslibs/timedeltas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ class Timedelta(_Timedelta):
11981198

11991199
kwargs = {key: _to_py_int_float(kwargs[key]) for key in kwargs}
12001200

1201-
nano = np.timedelta64(kwargs.pop('nanoseconds', 0), 'ns')
1201+
nano = convert_to_timedelta64(kwargs.pop('nanoseconds', 0), 'ns')
12021202
try:
12031203
value = nano + convert_to_timedelta64(timedelta(**kwargs),
12041204
'ns')

pandas/_testing.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,11 @@ def _get_ilevel_values(index, level):
706706
if isinstance(left, pd.PeriodIndex) or isinstance(right, pd.PeriodIndex):
707707
assert_attr_equal("freq", left, right, obj=obj)
708708
if isinstance(left, pd.IntervalIndex) or isinstance(right, pd.IntervalIndex):
709-
assert_interval_array_equal(left.values, right.values)
709+
assert_interval_array_equal(left._values, right._values)
710710

711711
if check_categorical:
712712
if is_categorical_dtype(left) or is_categorical_dtype(right):
713-
assert_categorical_equal(left.values, right.values, obj=f"{obj} category")
713+
assert_categorical_equal(left._values, right._values, obj=f"{obj} category")
714714

715715

716716
def assert_class_equal(left, right, exact: Union[bool, str] = True, obj="Input"):
@@ -883,7 +883,7 @@ def assert_interval_array_equal(left, right, exact="equiv", obj="IntervalArray")
883883
def assert_period_array_equal(left, right, obj="PeriodArray"):
884884
_check_isinstance(left, right, PeriodArray)
885885

886-
assert_numpy_array_equal(left._data, right._data, obj=f"{obj}.values")
886+
assert_numpy_array_equal(left._data, right._data, obj=f"{obj}._data")
887887
assert_attr_equal("freq", left, right, obj=obj)
888888

889889

@@ -1170,10 +1170,10 @@ def assert_series_equal(
11701170

11711171
# datetimelike may have different objects (e.g. datetime.datetime
11721172
# vs Timestamp) but will compare equal
1173-
if not Index(left.values).equals(Index(right.values)):
1173+
if not Index(left._values).equals(Index(right._values)):
11741174
msg = (
1175-
f"[datetimelike_compat=True] {left.values} "
1176-
f"is not equal to {right.values}."
1175+
f"[datetimelike_compat=True] {left._values} "
1176+
f"is not equal to {right._values}."
11771177
)
11781178
raise AssertionError(msg)
11791179
else:
@@ -1212,8 +1212,8 @@ def assert_series_equal(
12121212
if check_categorical:
12131213
if is_categorical_dtype(left) or is_categorical_dtype(right):
12141214
assert_categorical_equal(
1215-
left.values,
1216-
right.values,
1215+
left._values,
1216+
right._values,
12171217
obj=f"{obj} category",
12181218
check_category_order=check_category_order,
12191219
)

pandas/core/algorithms.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ def unique(values):
313313
314314
See Also
315315
--------
316-
Index.unique
317-
Series.unique
316+
Index.unique : Return unique values from an Index.
317+
Series.unique : Return unique values of Series object.
318318
319319
Examples
320320
--------
@@ -1515,7 +1515,7 @@ def take(arr, indices, axis: int = 0, allow_fill: bool = False, fill_value=None)
15151515
15161516
See Also
15171517
--------
1518-
numpy.take
1518+
numpy.take : Take elements from an array along an axis.
15191519
15201520
Examples
15211521
--------

pandas/core/base.py

+15-27
Original file line numberDiff line numberDiff line change
@@ -927,16 +927,17 @@ def max(self, axis=None, skipna=True, *args, **kwargs):
927927
nv.validate_max(args, kwargs)
928928
return nanops.nanmax(self._values, skipna=skipna)
929929

930+
@doc(op="max", oppose="min", value="largest")
930931
def argmax(self, axis=None, skipna=True, *args, **kwargs):
931932
"""
932-
Return int position of the largest value in the Series.
933+
Return int position of the {value} value in the Series.
933934
934-
If the maximum is achieved in multiple locations,
935+
If the {op}imum is achieved in multiple locations,
935936
the first row position is returned.
936937
937938
Parameters
938939
----------
939-
axis : {None}
940+
axis : {{None}}
940941
Dummy argument for consistency with Series.
941942
skipna : bool, default True
942943
Exclude NA/null values when showing the result.
@@ -946,21 +947,22 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
946947
Returns
947948
-------
948949
int
949-
Row position of the maximum values.
950+
Row position of the {op}imum value.
950951
951952
See Also
952953
--------
953-
numpy.ndarray.argmax : Equivalent method for numpy arrays.
954-
Series.argmin : Similar method, but returning the minimum.
954+
Series.arg{op} : Return position of the {op}imum value.
955+
Series.arg{oppose} : Return position of the {oppose}imum value.
956+
numpy.ndarray.arg{op} : Equivalent method for numpy arrays.
955957
Series.idxmax : Return index label of the maximum values.
956958
Series.idxmin : Return index label of the minimum values.
957959
958960
Examples
959961
--------
960962
Consider dataset containing cereal calories
961963
962-
>>> s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0,
963-
... 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0})
964+
>>> s = pd.Series({{'Corn Flakes': 100.0, 'Almond Delight': 110.0,
965+
... 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0}})
964966
>>> s
965967
Corn Flakes 100.0
966968
Almond Delight 110.0
@@ -970,8 +972,11 @@ def argmax(self, axis=None, skipna=True, *args, **kwargs):
970972
971973
>>> s.argmax()
972974
2
975+
>>> s.argmin()
976+
0
973977
974-
The maximum cereal calories is in the third element,
978+
The maximum cereal calories is the third element and
979+
the minimum cereal calories is the first element,
975980
since series is zero-indexed.
976981
"""
977982
nv.validate_minmax_axis(axis)
@@ -1019,25 +1024,8 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
10191024
nv.validate_min(args, kwargs)
10201025
return nanops.nanmin(self._values, skipna=skipna)
10211026

1027+
@doc(argmax, op="min", oppose="max", value="smallest")
10221028
def argmin(self, axis=None, skipna=True, *args, **kwargs):
1023-
"""
1024-
Return a ndarray of the minimum argument indexer.
1025-
1026-
Parameters
1027-
----------
1028-
axis : {None}
1029-
Dummy argument for consistency with Series.
1030-
skipna : bool, default True
1031-
1032-
Returns
1033-
-------
1034-
numpy.ndarray
1035-
1036-
See Also
1037-
--------
1038-
numpy.ndarray.argmin : Return indices of the minimum values along
1039-
the given axis.
1040-
"""
10411029
nv.validate_minmax_axis(axis)
10421030
nv.validate_argmax_with_skipna(skipna, args, kwargs)
10431031
return nanops.nanargmin(self._values, skipna=skipna)

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,11 @@ def try_timedelta(v):
11841184
from pandas import to_timedelta
11851185

11861186
try:
1187-
return to_timedelta(v)._ndarray_values.reshape(shape)
1187+
td_values = to_timedelta(v)
11881188
except ValueError:
11891189
return v.reshape(shape)
1190+
else:
1191+
return np.asarray(td_values).reshape(shape)
11901192

11911193
inferred_type = lib.infer_datetimelike_array(ensure_object(v))
11921194

pandas/core/frame.py

+33-46
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@
9494
)
9595
from pandas.core.dtypes.generic import (
9696
ABCDataFrame,
97-
ABCDatetimeIndex,
9897
ABCIndexClass,
9998
ABCMultiIndex,
100-
ABCPeriodIndex,
10199
ABCSeries,
102100
)
103101
from pandas.core.dtypes.missing import isna, notna
@@ -358,9 +356,9 @@ class DataFrame(NDFrame):
358356
--------
359357
DataFrame.from_records : Constructor from tuples, also record arrays.
360358
DataFrame.from_dict : From dicts of Series, arrays, or dicts.
361-
read_csv
362-
read_table
363-
read_clipboard
359+
read_csv : Read a comma-separated values (csv) file into DataFrame.
360+
read_table : Read general delimited file into DataFrame.
361+
read_clipboard : Read text from clipboard into DataFrame.
364362
365363
Examples
366364
--------
@@ -892,7 +890,7 @@ def style(self) -> "Styler":
892890
"""
893891

894892
@Appender(_shared_docs["items"])
895-
def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
893+
def items(self) -> Iterable[Tuple[Label, Series]]:
896894
if self.columns.is_unique and hasattr(self, "_item_cache"):
897895
for k in self.columns:
898896
yield k, self._get_item_cache(k)
@@ -901,10 +899,10 @@ def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
901899
yield k, self._ixs(i, axis=1)
902900

903901
@Appender(_shared_docs["items"])
904-
def iteritems(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
902+
def iteritems(self) -> Iterable[Tuple[Label, Series]]:
905903
yield from self.items()
906904

907-
def iterrows(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
905+
def iterrows(self) -> Iterable[Tuple[Label, Series]]:
908906
"""
909907
Iterate over DataFrame rows as (index, Series) pairs.
910908
@@ -4045,7 +4043,7 @@ def set_index(
40454043
"one-dimensional arrays."
40464044
)
40474045

4048-
missing: List[Optional[Hashable]] = []
4046+
missing: List[Label] = []
40494047
for col in keys:
40504048
if isinstance(
40514049
col, (ABCIndexClass, ABCSeries, np.ndarray, list, abc.Iterator)
@@ -4084,7 +4082,7 @@ def set_index(
40844082
else:
40854083
arrays.append(self.index)
40864084

4087-
to_remove: List[Optional[Hashable]] = []
4085+
to_remove: List[Label] = []
40884086
for col in keys:
40894087
if isinstance(col, ABCMultiIndex):
40904088
for n in range(col.nlevels):
@@ -4139,7 +4137,7 @@ def reset_index(
41394137
drop: bool = False,
41404138
inplace: bool = False,
41414139
col_level: Hashable = 0,
4142-
col_fill: Optional[Hashable] = "",
4140+
col_fill: Label = "",
41434141
) -> Optional["DataFrame"]:
41444142
"""
41454143
Reset the index, or a level of it.
@@ -4582,7 +4580,7 @@ def drop_duplicates(
45824580
duplicated = self.duplicated(subset, keep=keep)
45834581

45844582
if inplace:
4585-
(inds,) = (-duplicated)._ndarray_values.nonzero()
4583+
(inds,) = np.asarray(-duplicated).nonzero()
45864584
new_data = self._data.take(inds)
45874585

45884586
if ignore_index:
@@ -7393,8 +7391,9 @@ def corr(self, method="pearson", min_periods=1) -> "DataFrame":
73937391
73947392
See Also
73957393
--------
7396-
DataFrame.corrwith
7397-
Series.corr
7394+
DataFrame.corrwith : Compute pairwise correlation with another
7395+
DataFrame or Series.
7396+
Series.corr : Compute the correlation between two Series.
73987397
73997398
Examples
74007399
--------
@@ -7596,7 +7595,7 @@ def corrwith(self, other, axis=0, drop=False, method="pearson") -> Series:
75967595
75977596
See Also
75987597
--------
7599-
DataFrame.corr
7598+
DataFrame.corr : Compute pairwise correlation of columns.
76007599
"""
76017600
axis = self._get_axis_number(axis)
76027601
this = self._get_numeric_data()
@@ -8001,7 +8000,7 @@ def idxmin(self, axis=0, skipna=True) -> Series:
80018000
80028001
See Also
80038002
--------
8004-
Series.idxmin
8003+
Series.idxmin : Return index of the minimum element.
80058004
80068005
Notes
80078006
-----
@@ -8039,7 +8038,7 @@ def idxmax(self, axis=0, skipna=True) -> Series:
80398038
80408039
See Also
80418040
--------
8042-
Series.idxmax
8041+
Series.idxmax : Return index of the maximum element.
80438042
80448043
Notes
80458044
-----
@@ -8245,7 +8244,9 @@ def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation="linear"):
82458244

82468245
return result
82478246

8248-
def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame":
8247+
def to_timestamp(
8248+
self, freq=None, how: str = "start", axis: Axis = 0, copy: bool = True
8249+
) -> "DataFrame":
82498250
"""
82508251
Cast to DatetimeIndex of timestamps, at *beginning* of period.
82518252
@@ -8265,23 +8266,16 @@ def to_timestamp(self, freq=None, how="start", axis=0, copy=True) -> "DataFrame"
82658266
-------
82668267
DataFrame with DatetimeIndex
82678268
"""
8268-
new_data = self._data
8269-
if copy:
8270-
new_data = new_data.copy()
8269+
new_obj = self.copy(deep=copy)
82718270

8272-
axis = self._get_axis_number(axis)
8273-
if axis == 0:
8274-
assert isinstance(self.index, (ABCDatetimeIndex, ABCPeriodIndex))
8275-
new_data.set_axis(1, self.index.to_timestamp(freq=freq, how=how))
8276-
elif axis == 1:
8277-
assert isinstance(self.columns, (ABCDatetimeIndex, ABCPeriodIndex))
8278-
new_data.set_axis(0, self.columns.to_timestamp(freq=freq, how=how))
8279-
else: # pragma: no cover
8280-
raise AssertionError(f"Axis must be 0 or 1. Got {axis}")
8271+
axis_name = self._get_axis_name(axis)
8272+
old_ax = getattr(self, axis_name)
8273+
new_ax = old_ax.to_timestamp(freq=freq, how=how)
82818274

8282-
return self._constructor(new_data)
8275+
setattr(new_obj, axis_name, new_ax)
8276+
return new_obj
82838277

8284-
def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame":
8278+
def to_period(self, freq=None, axis: Axis = 0, copy: bool = True) -> "DataFrame":
82858279
"""
82868280
Convert DataFrame from DatetimeIndex to PeriodIndex.
82878281
@@ -8299,23 +8293,16 @@ def to_period(self, freq=None, axis=0, copy=True) -> "DataFrame":
82998293
83008294
Returns
83018295
-------
8302-
TimeSeries with PeriodIndex
8296+
DataFrame with PeriodIndex
83038297
"""
8304-
new_data = self._data
8305-
if copy:
8306-
new_data = new_data.copy()
8298+
new_obj = self.copy(deep=copy)
83078299

8308-
axis = self._get_axis_number(axis)
8309-
if axis == 0:
8310-
assert isinstance(self.index, ABCDatetimeIndex)
8311-
new_data.set_axis(1, self.index.to_period(freq=freq))
8312-
elif axis == 1:
8313-
assert isinstance(self.columns, ABCDatetimeIndex)
8314-
new_data.set_axis(0, self.columns.to_period(freq=freq))
8315-
else: # pragma: no cover
8316-
raise AssertionError(f"Axis must be 0 or 1. Got {axis}")
8300+
axis_name = self._get_axis_name(axis)
8301+
old_ax = getattr(self, axis_name)
8302+
new_ax = old_ax.to_period(freq=freq)
83178303

8318-
return self._constructor(new_data)
8304+
setattr(new_obj, axis_name, new_ax)
8305+
return new_obj
83198306

83208307
def isin(self, values) -> "DataFrame":
83218308
"""

0 commit comments

Comments
 (0)