Skip to content

TypeError in pandas._libs.tslibs.period._ordinal_from_fields #22067

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

Closed
aabdullah-bos opened this issue Jul 26, 2018 · 5 comments · Fixed by #22087
Closed

TypeError in pandas._libs.tslibs.period._ordinal_from_fields #22067

aabdullah-bos opened this issue Jul 26, 2018 · 5 comments · Fixed by #22087
Labels
Bug Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@aabdullah-bos
Copy link

Using pandas 0.23.3 I receive a TypeErorr when running the following code:

import pandas
ts = pandas.date_range('2017-01-01', '2018-01-01', freq='B')
months = ts.map(lambda x: pandas.Period(year=x.year, month=x.month, freq='M'))
# Screen output even though operation succeeds.
# TypeError                                 Traceback (most recent call last)
# TypeError: an integer is required
# Exception TypeError: 'an integer is required' in 'pandas._libs.tslibs.period._ordinal_from_fields' ignored

Oddly, enough the operation succeeds, months has the correct data.

Expected Output

I would expect no output to stderr or stdout, just the correct assignment of the map operation. This is more of an annoyance than anything else.

Output of pd.show_versions()

pandas.show_versions()

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

pandas: 0.23.3
pytest: None
pip: 18.0
setuptools: 40.0.0
Cython: None
numpy: 1.13.0
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 5.7.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: 3.4.4
numexpr: 2.6.6
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.2.10
pymysql: 0.9.2
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

[paste the output of pd.show_versions() here below this line]

@WillAyd
Copy link
Member

WillAyd commented Jul 26, 2018

Hmm strange indeed. Thanks for the report - investigation and PRs are always welcome!

@WillAyd WillAyd added Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas labels Jul 26, 2018
@mosrosem
Copy link

I couldn't replicate the error. This error message doesn't appear when I run it.

@WillAyd
Copy link
Member

WillAyd commented Jul 26, 2018

@mosrosem just to be clear it's not actually throwing the error (at least for me) but rather printing to STDOUT and continuing along as planned. If it's not doing for you can you post your pd.show_versions?

@jbrockmendel
Copy link
Member

jbrockmendel commented Jul 27, 2018

It looks like you've stumbled on something genuinely weird. Period.__new__ is calling _ordinal_from_fields, and on the first call (but not in subsequent calls), the year and month arguments are both Int64Index objects. So it looks like DatetimeIndex.map is doing doing something "creative".

@WillAyd any idea who to CC on this?

Update Actually not such a mystery, just weird. DatetimeIndexOpsMixin.map:

    # Try to run function on index first, and then on elements of index
    # Especially important for group-by functionality
    def map(self, f):
        try:
            result = f(self)

            # Try to use this result if we can
            if isinstance(result, np.ndarray):
                result = Index(result)

            if not isinstance(result, Index):
                raise TypeError('The map function must return an Index object')
            return result
        except Exception:
            return self.astype(object).map(f)

jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Jul 27, 2018
@WillAyd
Copy link
Member

WillAyd commented Jul 28, 2018

@jbrockmendel I was thinking of CC'ing you :-) looks like you might be on to something

@jreback jreback added this to the 0.24.0 milestone Jul 28, 2018
@jreback jreback added the Bug label Jul 28, 2018
jreback pushed a commit that referenced this issue Jul 30, 2018
dberenbaum pushed a commit to dberenbaum/pandas that referenced this issue Aug 3, 2018
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this issue Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants