Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructing PeriodIndex with string data #26109

Closed
mfaafm opened this issue Apr 16, 2019 · 3 comments · Fixed by #33304
Closed

Constructing PeriodIndex with string data #26109

mfaafm opened this issue Apr 16, 2019 · 3 comments · Fixed by #33304
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Period Period data type Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@mfaafm
Copy link

mfaafm commented Apr 16, 2019

Code Sample, a copy-pastable example if possible

# quarters as string representation
quarters = pd.Series(["2018Q1", "2018Q2", "2018Q1", "2018Q3"])

# gives TypeError (see details for stack trace)
pi = pd.PeriodIndex(quarters, freq="Q")
TypeError: Incorrect dtype

# works as intended
pi = pd.PeriodIndex(quarters.values, freq="Q")

Problem description

Creating a PeriodIndex with string data fails when using a pandas.Series but works if using the underlying np.array.

Is this expected behavior?

Output of stack trace

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-1d88474891ea> in <module>
----> 1 pi = pd.PeriodIndex(quarters, freq="Q")

/usr/local/anaconda3/envs/PCS/lib/python3.7/site-packages/pandas/core/indexes/period.py in __new__(cls, data, ordinal, freq, start, end, periods, tz, dtype, copy, name, **fields)
    238             else:
    239                 # don't pass copy here, since we copy later.
--> 240                 data = period_array(data=data, freq=freq)
    241 
    242         if copy:

/usr/local/anaconda3/envs/PCS/lib/python3.7/site-packages/pandas/core/arrays/period.py in period_array(data, freq, copy)
    767         return PeriodArray._from_datetime64(data, freq)
    768     if isinstance(data, (ABCPeriodIndex, ABCSeries, PeriodArray)):
--> 769         return PeriodArray(data, freq)
    770 
    771     # other iterable of some kind

/usr/local/anaconda3/envs/PCS/lib/python3.7/site-packages/pandas/core/arrays/period.py in __init__(self, values, freq, dtype, copy)
    155             values = values._values
    156             if not isinstance(values, type(self)):
--> 157                 raise TypeError("Incorrect dtype")
    158 
    159         elif isinstance(values, ABCPeriodIndex):

TypeError: Incorrect dtype

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.2.final.0
python-bits: 64
OS: Darwin
OS-release: 18.2.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.24.1
pytest: None
pip: 19.0.3
setuptools: 40.8.0
Cython: 0.29.5
numpy: 1.15.4
scipy: 1.2.1
pyarrow: 0.11.1
xarray: None
IPython: 7.3.0
sphinx: None
patsy: None
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.1
xlrd: 1.2.0
xlwt: None
xlsxwriter: None
lxml.etree: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None

@jorisvandenbossche jorisvandenbossche added Period Period data type Regression Functionality that used to work in a prior pandas version labels Apr 16, 2019
@jorisvandenbossche
Copy link
Member

@mfaafm Thanks for the report!

That's certainly not expected behaviour, and it is actually also a regression compared to 0.23.4.

@Alexis-Rivera
Copy link

Newbie question. Seems your values are duplicated in your quarters assignment. 2018Q1 is listed twice. Not sure if this impacts it or not, I’m still learning myself.

@jbrockmendel jbrockmendel added the Constructors Series/DataFrame/Index/pd.array Constructors label Jul 23, 2019
@simonjayhawkins
Copy link
Member

That's certainly not expected behaviour, and it is actually also a regression compared to 0.23.4.

caused by #22862 (i.e. 24.0)

ed29430 is the first bad commit
commit ed29430
Author: Tom Augspurger TomAugspurger@users.noreply.github.com
Date: Thu Oct 25 17:38:54 2018 -0500

REF: Make PeriodArray an ExtensionArray (#22862)

@jreback jreback added this to the 1.1 milestone Apr 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Period Period data type Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants