Skip to content

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index' #19352

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
monajalal opened this issue Jan 22, 2018 · 6 comments

Comments

@monajalal
Copy link

Please have a look
I converted the timezone using this code:

import pandas as pd
import matplotlib.pyplot as plt

#2016-10-20 03:43:11+00:00
tweets_df = pd.read_csv('valid_tweets.csv')

tweets_df['Time'] = pd.Index(pd.to_datetime(tweets_df['Time'], utc=True)).tz_localize('UTC').tz_convert('US/Eastern')

tweets_df.to_csv('converted_timezone_tweets.csv', index=False)

Then I used it in this code:

import pandas as pd
import matplotlib.pyplot as plt


time_interval = pd.offsets.Second(10)

fig, ax = plt.subplots(figsize=(6, 3.5))

ax = (
    pd.read_csv('converted_timezone_tweets.csv', engine='python', parse_dates=['Time'])
          .resample(time_interval, on='Time')['ID']
          .count()
          .plot.line(ax=ax)
)

plt.show()

I get

https://stackoverflow.com/questions/48389030/pd-read-csv-fails-after-converting-the-timezone
/scratch/sjn/anaconda/bin/python /scratch2/debate_tweets/temporal_analysis.py
Traceback (most recent call last):
  File "/scratch2/debate_tweets/temporal_analysis.py", line 11, in <module>
    .resample(time_interval, on='Time')['ID']
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/pandas/core/generic.py", line 4729, in resample
    base=base, key=on, level=level)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/pandas/core/resample.py", line 969, in resample
    return tg._get_resampler(obj, kind=kind)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/pandas/core/resample.py", line 1091, in _get_resampler
    "but got an instance of %r" % type(ax).__name__)
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

Process finished with exit code 1


pandas version 0.20.3 and in CentOS 7.

@chris-b1
Copy link
Contributor

Please try and make this a reproducible example - very likely 'Time' isn't being parsed as a datetime when you read it in the second time.

@monajalal
Copy link
Author

@monajalal
Copy link
Author

also @chris-b1 how should it be fixed?

@chris-b1
Copy link
Contributor

chris-b1 commented Jan 22, 2018

Guessing - you probably have some kind of embedded line break in your data, try the solution here
#11166 (comment) or please make a runnable reproduction in the issue - something I can copy and paste and run - either attach the actual file to the issue, or use StringIO with dummy data.

@jreback jreback added this to the No action milestone Jan 23, 2018
@jreback
Copy link
Contributor

jreback commented Jan 23, 2018

this is not reproducible and better asked on SO. The message is clear, you don't have the proper index.

@jreback jreback closed this as completed Jan 23, 2018
@HabibMCS
Copy link

I reproduced this, @jreback can you try to solve this. It is with proper index, (TIme Stamp).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants