Skip to content

Sync Fork from Upstream Repo #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3599,7 +3599,7 @@ def align(
see_also_sub=" or columns",
)
@Appender(NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis=0, inplace=False):
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
return super().set_axis(labels, axis=axis, inplace=inplace)

@Substitution(**_shared_doc_kwargs)
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def _obj_with_exclusions(self: FrameOrSeries) -> FrameOrSeries:
""" internal compat with SelectionMixin """
return self

def set_axis(self, labels, axis=0, inplace=False):
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
"""
Assign desired index to given axis.

Expand Down Expand Up @@ -561,7 +561,8 @@ def set_axis(self, labels, axis=0, inplace=False):
obj.set_axis(labels, axis=axis, inplace=True)
return obj

def _set_axis(self, axis, labels) -> None:
def _set_axis(self, axis: int, labels: Index) -> None:
labels = ensure_index(labels)
self._data.set_axis(axis, labels)
self._clear_item_cache()

Expand Down
20 changes: 10 additions & 10 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ def __nonzero__(self):
__bool__ = __nonzero__

@property
def shape(self):
def shape(self) -> Tuple[int, ...]:
return tuple(len(ax) for ax in self.axes)

@property
def ndim(self) -> int:
return len(self.axes)

def set_axis(self, axis, new_labels):
new_labels = ensure_index(new_labels)
def set_axis(self, axis: int, new_labels: Index):
# Caller is responsible for ensuring we have an Index object.
old_len = len(self.axes[axis])
new_len = len(new_labels)

Expand All @@ -184,7 +184,9 @@ def set_axis(self, axis, new_labels):

self.axes[axis] = new_labels

def rename_axis(self, mapper, axis, copy: bool = True, level=None):
def rename_axis(
self, mapper, axis: int, copy: bool = True, level=None
) -> "BlockManager":
"""
Rename one of axes.

Expand All @@ -193,7 +195,7 @@ def rename_axis(self, mapper, axis, copy: bool = True, level=None):
mapper : unary callable
axis : int
copy : bool, default True
level : int, default None
level : int or None, default None
"""
obj = self.copy(deep=copy)
obj.set_axis(axis, _transform_index(self.axes[axis], mapper, level))
Expand Down Expand Up @@ -233,7 +235,7 @@ def _rebuild_blknos_and_blklocs(self):
self._blklocs = new_blklocs

@property
def items(self):
def items(self) -> Index:
return self.axes[0]

def _get_counts(self, f):
Expand Down Expand Up @@ -623,7 +625,7 @@ def comp(s, regex=False):
bm._consolidate_inplace()
return bm

def is_consolidated(self):
def is_consolidated(self) -> bool:
"""
Return True if more than one block with the same dtype
"""
Expand Down Expand Up @@ -688,7 +690,7 @@ def get_numeric_data(self, copy: bool = False):
self._consolidate_inplace()
return self.combine([b for b in self.blocks if b.is_numeric], copy)

def combine(self, blocks, copy=True):
def combine(self, blocks: List[Block], copy: bool = True) -> "BlockManager":
""" return a new manager with the blocks """
if len(blocks) == 0:
return self.make_empty()
Expand Down Expand Up @@ -992,7 +994,6 @@ def delete(self, item):
self.blocks = tuple(
b for blkno, b in enumerate(self.blocks) if not is_blk_deleted[blkno]
)
self._shape = None
self._rebuild_blknos_and_blklocs()

def set(self, item, value):
Expand Down Expand Up @@ -1160,7 +1161,6 @@ def insert(self, loc: int, item, value, allow_duplicates: bool = False):

self.axes[0] = new_axis
self.blocks += (block,)
self._shape = None

self._known_consolidated = False

Expand Down
23 changes: 13 additions & 10 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
TYPE_CHECKING,
Any,
Callable,
Hashable,
Iterable,
List,
Optional,
Expand All @@ -23,7 +22,7 @@
from pandas._config import get_option

from pandas._libs import lib, properties, reshape, tslibs
from pandas._typing import Label
from pandas._typing import Axis, DtypeObj, Label
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, doc
from pandas.util._validators import validate_bool_kwarg, validate_percentile
Expand Down Expand Up @@ -177,7 +176,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame):

_typ = "series"

_name: Optional[Hashable]
_name: Label
_metadata: List[str] = ["name"]
_internal_names_set = {"index"} | generic.NDFrame._internal_names_set
_accessors = {"dt", "cat", "str", "sparse"}
Expand Down Expand Up @@ -391,9 +390,12 @@ def _can_hold_na(self):

_index = None

def _set_axis(self, axis, labels, fastpath: bool = False) -> None:
def _set_axis(self, axis: int, labels, fastpath: bool = False) -> None:
"""
Override generic, we want to set the _typ here.

This is called from the cython code when we set the `index` attribute
directly, e.g. `series.index = [1, 2, 3]`.
"""
if not fastpath:
labels = ensure_index(labels)
Expand All @@ -413,6 +415,7 @@ def _set_axis(self, axis, labels, fastpath: bool = False) -> None:

object.__setattr__(self, "_index", labels)
if not fastpath:
# The ensure_index call aabove ensures we have an Index object
self._data.set_axis(axis, labels)

def _update_inplace(self, result, **kwargs):
Expand All @@ -421,25 +424,25 @@ def _update_inplace(self, result, **kwargs):

# ndarray compatibility
@property
def dtype(self):
def dtype(self) -> DtypeObj:
"""
Return the dtype object of the underlying data.
"""
return self._data.dtype

@property
def dtypes(self):
def dtypes(self) -> DtypeObj:
"""
Return the dtype object of the underlying data.
"""
return self._data.dtype

@property
def name(self) -> Optional[Hashable]:
def name(self) -> Label:
return self._name

@name.setter
def name(self, value: Optional[Hashable]) -> None:
def name(self, value: Label) -> None:
if not is_hashable(value):
raise TypeError("Series.name must be a hashable type")
object.__setattr__(self, "_name", value)
Expand Down Expand Up @@ -689,7 +692,7 @@ def __array_ufunc__(
inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
result = getattr(ufunc, method)(*inputs, **kwargs)

name: Optional[Hashable]
name: Label
if len(set(names)) == 1:
name = names[0]
else:
Expand Down Expand Up @@ -3983,7 +3986,7 @@ def rename(
see_also_sub="",
)
@Appender(generic.NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis=0, inplace=False):
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
return super().set_axis(labels, axis=axis, inplace=inplace)

@Substitution(**_shared_doc_kwargs)
Expand Down
73 changes: 1 addition & 72 deletions pandas/tests/frame/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pandas._testing as tm


class TestDataFrameConcatCommon:
class TestDataFrameConcat:
def test_concat_multiple_frames_dtypes(self):

# GH 2759
Expand Down Expand Up @@ -107,77 +107,6 @@ def test_concat_tuple_keys(self):
)
tm.assert_frame_equal(results, expected)

def test_join_str_datetime(self):
str_dates = ["20120209", "20120222"]
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]

A = DataFrame(str_dates, index=range(2), columns=["aa"])
C = DataFrame([[1, 2], [3, 4]], index=str_dates, columns=dt_dates)

tst = A.join(C, on="aa")

assert len(tst.columns) == 3

def test_join_multiindex_leftright(self):
# GH 10741
df1 = pd.DataFrame(
[
["a", "x", 0.471780],
["a", "y", 0.774908],
["a", "z", 0.563634],
["b", "x", -0.353756],
["b", "y", 0.368062],
["b", "z", -1.721840],
["c", "x", 1],
["c", "y", 2],
["c", "z", 3],
],
columns=["first", "second", "value1"],
).set_index(["first", "second"])

df2 = pd.DataFrame(
[["a", 10], ["b", 20]], columns=["first", "value2"]
).set_index(["first"])

exp = pd.DataFrame(
[
[0.471780, 10],
[0.774908, 10],
[0.563634, 10],
[-0.353756, 20],
[0.368062, 20],
[-1.721840, 20],
[1.000000, np.nan],
[2.000000, np.nan],
[3.000000, np.nan],
],
index=df1.index,
columns=["value1", "value2"],
)

# these must be the same results (but columns are flipped)
tm.assert_frame_equal(df1.join(df2, how="left"), exp)
tm.assert_frame_equal(df2.join(df1, how="right"), exp[["value2", "value1"]])

exp_idx = pd.MultiIndex.from_product(
[["a", "b"], ["x", "y", "z"]], names=["first", "second"]
)
exp = pd.DataFrame(
[
[0.471780, 10],
[0.774908, 10],
[0.563634, 10],
[-0.353756, 20],
[0.368062, 20],
[-1.721840, 20],
],
index=exp_idx,
columns=["value1", "value2"],
)

tm.assert_frame_equal(df1.join(df2, how="right"), exp)
tm.assert_frame_equal(df2.join(df1, how="left"), exp[["value2", "value1"]])

def test_concat_named_keys(self):
# GH 14252
df = pd.DataFrame({"foo": [1, 2], "bar": [0.1, 0.2]})
Expand Down
76 changes: 76 additions & 0 deletions pandas/tests/frame/test_join.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import datetime

import numpy as np
import pytest

import pandas as pd
from pandas import DataFrame, Index, period_range
import pandas._testing as tm

Expand Down Expand Up @@ -216,3 +219,76 @@ def test_suppress_future_warning_with_sort_kw(sort_kw):
with tm.assert_produces_warning(None, check_stacklevel=False):
result = a.join([b, c], how="outer", sort=sort_kw)
tm.assert_frame_equal(result, expected)


class TestDataFrameJoin:
def test_join_str_datetime(self):
str_dates = ["20120209", "20120222"]
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]

A = DataFrame(str_dates, index=range(2), columns=["aa"])
C = DataFrame([[1, 2], [3, 4]], index=str_dates, columns=dt_dates)

tst = A.join(C, on="aa")

assert len(tst.columns) == 3

def test_join_multiindex_leftright(self):
# GH 10741
df1 = pd.DataFrame(
[
["a", "x", 0.471780],
["a", "y", 0.774908],
["a", "z", 0.563634],
["b", "x", -0.353756],
["b", "y", 0.368062],
["b", "z", -1.721840],
["c", "x", 1],
["c", "y", 2],
["c", "z", 3],
],
columns=["first", "second", "value1"],
).set_index(["first", "second"])

df2 = pd.DataFrame(
[["a", 10], ["b", 20]], columns=["first", "value2"]
).set_index(["first"])

exp = pd.DataFrame(
[
[0.471780, 10],
[0.774908, 10],
[0.563634, 10],
[-0.353756, 20],
[0.368062, 20],
[-1.721840, 20],
[1.000000, np.nan],
[2.000000, np.nan],
[3.000000, np.nan],
],
index=df1.index,
columns=["value1", "value2"],
)

# these must be the same results (but columns are flipped)
tm.assert_frame_equal(df1.join(df2, how="left"), exp)
tm.assert_frame_equal(df2.join(df1, how="right"), exp[["value2", "value1"]])

exp_idx = pd.MultiIndex.from_product(
[["a", "b"], ["x", "y", "z"]], names=["first", "second"]
)
exp = pd.DataFrame(
[
[0.471780, 10],
[0.774908, 10],
[0.563634, 10],
[-0.353756, 20],
[0.368062, 20],
[-1.721840, 20],
],
index=exp_idx,
columns=["value1", "value2"],
)

tm.assert_frame_equal(df1.join(df2, how="right"), exp)
tm.assert_frame_equal(df2.join(df1, how="left"), exp[["value2", "value1"]])
2 changes: 1 addition & 1 deletion pandas/tests/series/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pandas import Series


class TestSeriesCombine:
class TestSeriesConcat:
@pytest.mark.parametrize(
"dtype", ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"]
)
Expand Down