Skip to content

statsmodels.api.datasets issue #4775

Closed
Closed

Description

@atodniAr

Issue

import pandas as pd
import statsmodels.api as sm

data = sm.datasets.co2.load_pandas()

returns

/usr/local/lib/python3.6/site-packages/statsmodels/datasets/co2/data.py in load_pandas()
     64     index = pd.DatetimeIndex(start=data.data['date'][0].decode('utf-8'),
     65                              periods=len(data.data), format='%Y%m%d',
---> 66                              freq='W-SAT')
     67     dataset = pd.DataFrame(data.data['co2'], index=index, columns=['co2'])
     68     #NOTE: this is how I got the missing values in co2.csv

TypeError: __new__() got an unexpected keyword argument 'format'

Fix

Seems pd.DatetimeIndex() has no argument 'format'. My code worked now after deleting the format argument.
The strange thing is this code worked one month ago, when I should have pandas 0.22.0, but I can't find this argument in the pandas documents for this patch as well.

Activity

  1. harjain99 commented on Jul 14, 2018

    @harjain99

    Hi,
    I've been getting the same error and I already tried the fix that you mentioned but it doesn't seem to work.
    Please let me know if you have any solutions

    `def load_pandas():
    data = load()

    # pandas <= 0.12.0 fails in the to_datetime regex on Python 3
    
    index = pd.DatetimeIndex(start=data.data['date'][0].decode('utf-8'), periods=len(data.data), freq='W-SAT')
    
    dataset = pd.DataFrame(data.data['co2'], index=index, columns=['co2'])
    
    #NOTE: this is how I got the missing values in co2.csv
    
    #new_index = pd.DatetimeIndex(start='1958-3-29', end=index[-1],
                                 freq='W-SAT')
    
    #data.data = dataset.reindex(new_index)
    
    data.data = dataset
    
    return data`
    

    Any kind of help would be appreciated. Thanks in advance.

  2. atodniAr commented on Jul 24, 2018

    @atodniAr
    Author

    Hi @harjain99

    Sorry for a late reply. I'm not sure about the error you got. Can you include the error message python returned?

  3. harjain99 commented on Jul 28, 2018

    @harjain99

    Hi @atodniAr,
    Its the exact same error as you got :(

  4. bashtage commented on Aug 2, 2018

    @bashtage
    Member

    This is old statsmodels. Please update to 0.9.0.

  5. acbecker commented on Aug 2, 2018

    @acbecker

    I can recreate with:

    conda 4.5.9 py36_0
    python 3.6.5 hc3d631a_2
    statsmodels 0.9.0 py36h035aef0_0
    pandas 0.23.3 py36h04863e7_0

    A quick workaround is

        index = pd.DatetimeIndex(start=data.data['date'][0].decode('utf-8'),
                                 periods=len(data.data),
                                 freq='W-SAT').strftime('%Y%m%d')
    
  6. bashtage commented on Aug 3, 2018

    @bashtage
    Member

    You are right -- it is fixed in master but not in a released version.

  7. Interesting6 commented on Jan 22, 2019

    @Interesting6

    This is old statsmodels. Please update to 0.9.0.

    My statsmodels's version is 0.9.0, however it doesn't work and return the same error.

    And I use acbecker's method solved it.

    Besides, can I complain about the statsmodels's "api" mode?😂
    When I first use this module, I can see the "api" almost everywhere. But when I read the document of module, I can hardly see the "api". So I'm very confused what the difference between "import statsmodel.api as sm; sm.tsa.ARMA", "import statsmodel.tsa.api as smt; smt.ARMA" and "import statsmodel as sm; sm.tsa.arima_model.ARMA". 🙈
    After I turn to read the site-packge, I realized they are equality. But now, I think using "api" also make me bit little puzzled. so I prefer to use "from statsmodel.tsa import arima_model as arimapy; arimapy.ARMA".

  8. josef-pkt commented on Jan 22, 2019

    @josef-pkt
    Member

    @Interesting6 see http://www.statsmodels.org/devel/importpaths.html
    It's too make two different types of working with statsmodels easier.
    (I almost never import the api because the import time is too long for repeated restart of the python process during testing, in notebooks or in spyder. But many users want to have everything imported and available at once, which works well during longer interactive sessions.)

  9. josef-pkt commented on Jan 22, 2019

    @josef-pkt
    Member

    One advantage of the api, expecially when new to statsmodels is that tab completion already shows what's available. Without api, you need to know the module where a function is officially () located.
    (
    ) the actual module might be private, or in the sandbox.

  10. Interesting6 commented on Jan 31, 2019

    @Interesting6

    OK. Thank you.

  11. AVI18794 commented on Feb 11, 2019

    @AVI18794

    I got the same error even my statsmodels version is 0.9.0. Here is the attached screenshot of my code.
    image

  12. Matt-V50 commented on Feb 27, 2019

    @Matt-V50

    please add

    statsmodels
    pandas==0.22.0
    

    in requirements.txt under project directory and install (pycharm will popup some tip)

  13. bhishanpdl commented on Mar 26, 2019

    @bhishanpdl

    For the following configuration I got the same error and also FIXED IT indirectly:

    pandas      0.24.2
    statsmodels 0.9.0
    numpy       1.16.
    
    sm.datasets.co2.load_pandas().data

    Error:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-17-d6eff66314c5> in <module>
    ----> 1 sm.datasets.co2.load_pandas().data
    
    ~/miniconda3/envs/tsa/lib/python3.7/site-packages/statsmodels/datasets/co2/data.py in load_pandas()
         64     index = pd.DatetimeIndex(start=data.data['date'][0].decode('utf-8'),
         65                              periods=len(data.data), format='%Y%m%d',
    ---> 66                              freq='W-SAT')
         67     dataset = pd.DataFrame(data.data['co2'], index=index, columns=['co2'])
         68     #NOTE: this is how I got the missing values in co2.csv
    
    TypeError: __new__() got an unexpected keyword argument 'format'

    Solution
    Use numpy.rec instead of direct pandas:

    df = pd.DataFrame.from_records(sm.datasets.co2.load().data)
    df['date'] = df.date.apply(lambda x: x.decode('utf-8'))
    df['date'] = pd.to_datetime(df.date, format='%Y%m%d')
    df.set_index('date')
  14. bashtage commented on Jul 19, 2019

    @bashtage
    Member

    Fixed. in master.

  15. added this to the 0.11 milestone on Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions