From fbc5f20d35c0d031fc30dfe2cd8e1dfb21c8610a Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Tue, 28 Mar 2023 23:42:14 +0530 Subject: [PATCH] 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 --- pandas-stubs/core/arrays/base.pyi | 1 + pandas-stubs/core/construction.pyi | 4 ++-- tests/test_extension.py | 11 +++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pandas-stubs/core/arrays/base.pyi b/pandas-stubs/core/arrays/base.pyi index 3694c0b9..7141f70f 100644 --- a/pandas-stubs/core/arrays/base.pyi +++ b/pandas-stubs/core/arrays/base.pyi @@ -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 diff --git a/pandas-stubs/core/construction.pyi b/pandas-stubs/core/construction.pyi index 6b0cecdf..09c67187 100644 --- a/pandas-stubs/core/construction.pyi +++ b/pandas-stubs/core/construction.pyi @@ -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 @@ -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 = ... diff --git a/tests/test_extension.py b/tests/test_extension.py index 8714adfb..6784819d 100644 --- a/tests/test_extension.py +++ b/tests/test_extension.py @@ -1,5 +1,6 @@ import decimal +import pandas as pd from typing_extensions import assert_type from tests import check @@ -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)