Skip to content

Commit

Permalink
gh-583:added tolist() (#589)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ramvikrams authored Mar 28, 2023
1 parent 9885f0f commit fbc5f20
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions pandas-stubs/core/arrays/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ExtensionArray:
def copy(self) -> Self: ...
def view(self, dtype=...) -> Self | np.ndarray: ...
def ravel(self, order=...) -> Self: ...
def tolist(self) -> list: ...

class ExtensionOpsMixin:
@classmethod
Expand Down
4 changes: 2 additions & 2 deletions pandas-stubs/core/construction.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Sequence

import numpy as np
from pandas.core.arrays.base import ExtensionArray
from pandas.core.indexes.api import Index
from pandas.core.series import Series

Expand All @@ -10,13 +11,12 @@ from pandas._typing import (
)

from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import ABCExtensionArray

def array(
data: Sequence[object],
dtype: str | np.dtype | ExtensionDtype | None = ...,
copy: bool = ...,
) -> ABCExtensionArray: ...
) -> ExtensionArray: ...
def extract_array(obj, extract_numpy: bool = ...): ...
def sanitize_array(
data, index, dtype=..., copy: bool = ..., raise_cast_failure: bool = ...
Expand Down
11 changes: 11 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import decimal

import pandas as pd
from typing_extensions import assert_type

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

check(assert_type(arr, DecimalArray), DecimalArray, decimal.Decimal)
check(assert_type(arr.dtype, DecimalDtype), DecimalDtype)


def test_tolist() -> None:
data = {"State": "Texas", "Population": 2000000, "GDP": "2T"}
s = pd.Series(data)
data1 = [1, 2, 3]
s1 = pd.Series(data1)
check(assert_type(s.array.tolist(), list), list)
check(assert_type(s1.array.tolist(), list), list)
check(assert_type(pd.array([1, 2, 3]).tolist(), list), list)

0 comments on commit fbc5f20

Please sign in to comment.