Skip to content

Commit fe06de4

Browse files
committed
inference
1 parent d58a320 commit fe06de4

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

pandas/core/arrays/array_.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import numpy as np
22

3+
from pandas._libs import lib, tslibs
4+
35
from pandas.core.dtypes.common import is_extension_array_dtype
46
from pandas.core.dtypes.dtypes import registry
57
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
@@ -56,6 +58,8 @@ def array(data, dtype=None, copy=False):
5658
[a, b, a]
5759
Categories (3, object): [a < b < c]
5860
"""
61+
from pandas.core.arrays import period_array
62+
5963
if isinstance(data, (ABCSeries, ABCIndexClass)):
6064
data = data._values
6165

@@ -66,4 +70,15 @@ def array(data, dtype=None, copy=False):
6670
cls = dtype.construct_array_type()
6771
return cls._from_sequence(data, dtype=dtype, copy=copy)
6872

73+
if dtype is None:
74+
inferred_dtype = lib.infer_dtype(data)
75+
if inferred_dtype == 'period':
76+
try:
77+
return period_array(data)
78+
except tslibs.IncompatibleFrequency:
79+
pass # we return an array below.
80+
81+
# TODO(DatetimeArray): handle this type
82+
# TODO(BooleanArray): handle this type
83+
6984
return np.array(data, dtype=dtype, copy=copy)

pandas/tests/arrays/test_array.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import decimal
22

33
import numpy as np
4-
import pytest
54

6-
import pandas as pd
7-
import pandas.util.testing as tm
85
from pandas.core.dtypes.dtypes import registry
6+
7+
import pandas as pd
98
from pandas.api.extensions import register_extension_dtype
10-
from pandas.core.arrays import period_array, integer_array
9+
from pandas.core.arrays import integer_array, period_array
1110
from pandas.tests.extension.decimal import (
12-
to_decimal, DecimalArray, DecimalDtype
13-
)
11+
DecimalArray, DecimalDtype, to_decimal)
12+
import pandas.util.testing as tm
13+
import pytest
1414

1515

1616
@pytest.mark.parametrize("data, dtype, expected", [
@@ -38,6 +38,22 @@ def test_array(data, dtype, expected):
3838
tm.assert_equal(result, expected)
3939

4040

41+
@pytest.mark.parametrize('data, expected', [
42+
([pd.Period("2000", "D"), pd.Period("2001", "D")],
43+
period_array(["2000", "2001"], freq="D")),
44+
])
45+
def test_array_inference(data, expected):
46+
result = pd.array(data)
47+
tm.assert_equal(result, expected)
48+
49+
50+
def test_array_inference_period_fails():
51+
data = [pd.Period("2000", "D"), pd.Period("2001", "A")]
52+
result = pd.array(data)
53+
expected = np.array(data, dtype=object)
54+
tm.assert_numpy_array_equal(result, expected)
55+
56+
4157
# ---------------------------------------------------------------------------
4258
# A couple dummy classes to ensure that Series and Indexes are unboxed before
4359
# getting to the EA classes.

0 commit comments

Comments
 (0)