Skip to content

Commit fbc5f20

Browse files
authored
gh-583:added tolist() (#589)
* added tolist() * Update test_pandas.py * added the example * Update test_pandas.py * Update test_pandas.py * added the test * changed return type in array func and corrected the test
1 parent 9885f0f commit fbc5f20

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

pandas-stubs/core/arrays/base.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class ExtensionArray:
5555
def copy(self) -> Self: ...
5656
def view(self, dtype=...) -> Self | np.ndarray: ...
5757
def ravel(self, order=...) -> Self: ...
58+
def tolist(self) -> list: ...
5859

5960
class ExtensionOpsMixin:
6061
@classmethod

pandas-stubs/core/construction.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections.abc import Sequence
22

33
import numpy as np
4+
from pandas.core.arrays.base import ExtensionArray
45
from pandas.core.indexes.api import Index
56
from pandas.core.series import Series
67

@@ -10,13 +11,12 @@ from pandas._typing import (
1011
)
1112

1213
from pandas.core.dtypes.dtypes import ExtensionDtype
13-
from pandas.core.dtypes.generic import ABCExtensionArray
1414

1515
def array(
1616
data: Sequence[object],
1717
dtype: str | np.dtype | ExtensionDtype | None = ...,
1818
copy: bool = ...,
19-
) -> ABCExtensionArray: ...
19+
) -> ExtensionArray: ...
2020
def extract_array(obj, extract_numpy: bool = ...): ...
2121
def sanitize_array(
2222
data, index, dtype=..., copy: bool = ..., raise_cast_failure: bool = ...

tests/test_extension.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import decimal
22

3+
import pandas as pd
34
from typing_extensions import assert_type
45

56
from tests import check
@@ -14,3 +15,13 @@ def test_constructor() -> None:
1415

1516
check(assert_type(arr, DecimalArray), DecimalArray, decimal.Decimal)
1617
check(assert_type(arr.dtype, DecimalDtype), DecimalDtype)
18+
19+
20+
def test_tolist() -> None:
21+
data = {"State": "Texas", "Population": 2000000, "GDP": "2T"}
22+
s = pd.Series(data)
23+
data1 = [1, 2, 3]
24+
s1 = pd.Series(data1)
25+
check(assert_type(s.array.tolist(), list), list)
26+
check(assert_type(s1.array.tolist(), list), list)
27+
check(assert_type(pd.array([1, 2, 3]).tolist(), list), list)

0 commit comments

Comments
 (0)