Skip to content

Commit eef5694

Browse files
committed
Merge remote-tracking branch 'upstream/main' into natype-arithemtic
2 parents 6760c3d + 3033eea commit eef5694

File tree

5 files changed

+177
-38
lines changed

5 files changed

+177
-38
lines changed

pandas-stubs/_typing.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ IndexingInt: TypeAlias = (
893893
)
894894

895895
# AxesData is used for data for Index
896-
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView
896+
AxesData: TypeAlias = Mapping[S3, Any] | Axes | KeysView[S3]
897897

898898
# Any plain Python or numpy function
899899
Function: TypeAlias = np.ufunc | Callable[..., Any]

pandas-stubs/core/frame.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,16 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
545545
@classmethod
546546
def from_records(
547547
cls,
548-
data,
549-
index=...,
550-
exclude: SequenceNotStr[str] | None = None,
551-
columns: SequenceNotStr[str] | None = None,
548+
data: (
549+
np_2darray
550+
| Sequence[SequenceNotStr]
551+
| Sequence[Mapping[str, Any]]
552+
| Mapping[str, Any]
553+
| Mapping[str, SequenceNotStr[Any]]
554+
),
555+
index: str | SequenceNotStr[Hashable] | None = None,
556+
columns: ListLike | None = None,
557+
exclude: ListLike | None = None,
552558
coerce_float: bool = False,
553559
nrows: int | None = None,
554560
) -> Self: ...

pandas-stubs/core/indexes/period.pyi

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Hashable
22
import datetime
33
from typing import (
4-
final,
4+
Any,
55
overload,
66
)
77

@@ -14,27 +14,28 @@ from pandas.core.indexes.timedeltas import TimedeltaIndex
1414
from typing_extensions import Self
1515

1616
from pandas._libs.tslibs import (
17-
BaseOffset,
1817
NaTType,
1918
Period,
2019
)
2120
from pandas._libs.tslibs.period import _PeriodAddSub
21+
from pandas._typing import (
22+
AxesData,
23+
Dtype,
24+
Frequency,
25+
np_1darray,
26+
)
2227

2328
class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexFieldOps):
2429
def __new__(
2530
cls,
26-
data=...,
27-
ordinal=...,
28-
freq=...,
29-
tz=...,
30-
dtype=...,
31-
copy: bool = ...,
32-
name: Hashable = ...,
33-
**fields,
34-
): ...
31+
data: AxesData[Any] | None = None,
32+
freq: Frequency | None = None,
33+
dtype: Dtype | None = None,
34+
copy: bool = False,
35+
name: Hashable | None = None,
36+
) -> Self: ...
3537
@property
36-
def values(self): ...
37-
def __contains__(self, key) -> bool: ...
38+
def values(self) -> np_1darray[np.object_]: ...
3839
@overload
3940
def __sub__(self, other: Period) -> Index: ...
4041
@overload
@@ -53,31 +54,18 @@ class PeriodIndex(DatetimeIndexOpsMixin[pd.Period, np.object_], PeriodIndexField
5354
def __rsub__( # pyright: ignore[reportIncompatibleMethodOverride]
5455
self, other: NaTType
5556
) -> NaTType: ...
56-
@final
57-
def __array_wrap__(self, result, context=...): ...
58-
def asof_locs(self, where, mask): ...
59-
def searchsorted(self, value, side: str = ..., sorter=...): ...
57+
def asof_locs(
58+
self,
59+
where: pd.DatetimeIndex | PeriodIndex,
60+
mask: np_1darray[np.bool_],
61+
) -> np_1darray[np.intp]: ...
6062
@property
6163
def is_full(self) -> bool: ...
6264
@property
6365
def inferred_type(self) -> str: ...
64-
@final
65-
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
66-
def get_indexer_non_unique(self, target): ...
67-
def insert(self, loc, item): ...
68-
@final
69-
def join(
70-
self,
71-
other,
72-
*,
73-
how: str = ...,
74-
level=...,
75-
return_indexers: bool = ...,
76-
sort: bool = ...,
77-
): ...
7866
@property
7967
def freqstr(self) -> str: ...
80-
def shift(self, periods: int = 1, freq=...) -> Self: ...
68+
def shift(self, periods: int = 1, freq: Frequency | None = None) -> Self: ...
8169

8270
def period_range(
8371
start: (
@@ -87,6 +75,6 @@ def period_range(
8775
str | datetime.datetime | datetime.date | pd.Timestamp | pd.Period | None
8876
) = None,
8977
periods: int | None = None,
90-
freq: str | BaseOffset | None = None,
78+
freq: Frequency | None = None,
9179
name: Hashable | None = None,
9280
) -> PeriodIndex: ...

tests/test_frame.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4688,3 +4688,125 @@ def test_unstack() -> None:
46884688
),
46894689
pd.DataFrame,
46904690
)
4691+
4692+
4693+
def test_from_records() -> None:
4694+
4695+
# test with np.ndarray
4696+
arr = np.array([[1, "a"], [2, "b"]], dtype=object).reshape(2, 2)
4697+
check(assert_type(pd.DataFrame.from_records(arr), pd.DataFrame), pd.DataFrame)
4698+
4699+
# testing with list of tuples
4700+
data_tuples = [(1, "a"), (2, "b"), (3, "c")]
4701+
check(
4702+
assert_type(
4703+
pd.DataFrame.from_records(data_tuples, columns=["id", "name"]),
4704+
pd.DataFrame,
4705+
),
4706+
pd.DataFrame,
4707+
)
4708+
4709+
# testing with pd.Index as columns parameter
4710+
check(
4711+
assert_type(
4712+
pd.DataFrame.from_records(data_tuples, columns=pd.Index(["id", "name"])),
4713+
pd.DataFrame,
4714+
),
4715+
pd.DataFrame,
4716+
)
4717+
4718+
# Testing with list of tuples (instead of structured array for type compatibility)
4719+
data_array_tuples = [(1, "a"), (2, "b")]
4720+
check(
4721+
assert_type(
4722+
pd.DataFrame.from_records(data_array_tuples, columns=["id", "name"]),
4723+
pd.DataFrame,
4724+
),
4725+
pd.DataFrame,
4726+
)
4727+
4728+
# testing with list of dictionaries
4729+
data_dict_list = [{"id": 1, "name": "a"}, {"id": 2, "name": "b"}]
4730+
check(
4731+
assert_type(
4732+
pd.DataFrame.from_records(data_dict_list, columns=["id", "name"]),
4733+
pd.DataFrame,
4734+
),
4735+
pd.DataFrame,
4736+
)
4737+
4738+
# test with single dictionary
4739+
data_single_dict = {"id": 1, "name": "a"}
4740+
check(
4741+
assert_type(
4742+
pd.DataFrame.from_records(data_single_dict, index=["0"]), pd.DataFrame
4743+
),
4744+
pd.DataFrame,
4745+
)
4746+
4747+
# testing with mapping of sequences
4748+
data_mapping_dict = {"id": [1, 2], "name": ["a", "b"]}
4749+
check(
4750+
assert_type(pd.DataFrame.from_records(data_mapping_dict), pd.DataFrame),
4751+
pd.DataFrame,
4752+
)
4753+
4754+
# Testing with index parameter as string
4755+
check(
4756+
assert_type(
4757+
pd.DataFrame.from_records(data_tuples, columns=["id", "name"], index="id"),
4758+
pd.DataFrame,
4759+
),
4760+
pd.DataFrame,
4761+
)
4762+
4763+
# Testing with index parameter as sequence
4764+
check(
4765+
assert_type(
4766+
pd.DataFrame.from_records(
4767+
data_tuples, columns=["id", "name"], index=["id"]
4768+
),
4769+
pd.DataFrame,
4770+
),
4771+
pd.DataFrame,
4772+
)
4773+
4774+
# Testing with exclude parameter
4775+
check(
4776+
assert_type(
4777+
pd.DataFrame.from_records(
4778+
[(1, "a", "extra"), (2, "b", "extra")],
4779+
columns=["id", "name", "extra"],
4780+
exclude=["extra"],
4781+
),
4782+
pd.DataFrame,
4783+
),
4784+
pd.DataFrame,
4785+
)
4786+
4787+
# Testing with all parameters
4788+
check(
4789+
assert_type(
4790+
pd.DataFrame.from_records(
4791+
data_tuples,
4792+
index=None,
4793+
columns=["id", "name"],
4794+
exclude=None,
4795+
coerce_float=True,
4796+
nrows=2,
4797+
),
4798+
pd.DataFrame,
4799+
),
4800+
pd.DataFrame,
4801+
)
4802+
4803+
# Testing parameter order
4804+
check(
4805+
assert_type(
4806+
pd.DataFrame.from_records(
4807+
data_tuples, columns=["id", "name"], exclude=None
4808+
),
4809+
pd.DataFrame,
4810+
),
4811+
pd.DataFrame,
4812+
)

tests/test_indexes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,3 +1489,26 @@ def test_index_naming() -> None:
14891489
check(assert_type(df.index.names, list[Hashable | None]), list)
14901490
df.index.names = (None,)
14911491
check(assert_type(df.index.names, list[Hashable | None]), list)
1492+
1493+
1494+
def test_period_index_constructor() -> None:
1495+
check(
1496+
assert_type(pd.PeriodIndex(["2000"], dtype="period[D]"), pd.PeriodIndex),
1497+
pd.PeriodIndex,
1498+
)
1499+
check(
1500+
assert_type(
1501+
pd.PeriodIndex(["2000"], freq="D", name="foo", copy=True), pd.PeriodIndex
1502+
),
1503+
pd.PeriodIndex,
1504+
)
1505+
1506+
1507+
def test_period_index_asof_locs() -> None:
1508+
idx = pd.PeriodIndex(["2000", "2001"], freq="D")
1509+
where = pd.DatetimeIndex(["2023-05-30 00:12:00", "2023-06-01 00:00:00"])
1510+
mask = np.ones(2, dtype=bool)
1511+
check(
1512+
assert_type(idx.asof_locs(where, mask), np_1darray[np.intp]),
1513+
np_1darray[np.intp],
1514+
)

0 commit comments

Comments
 (0)