question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jrebackcommented, Jan 23, 2018

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

0reactions
chris-b1commented, Jan 22, 2018

Guessing - you probably have some kind of embedded line break in your data, try the solution here https://github.com/pandas-dev/pandas/issues/11166#issuecomment-142465152 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex ...
But I got This Error: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Int64Index'.
Read more >
Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex ...
This error happens when you use a pandas method which is only supposed to work with a datetime index. For instance, you might...
Read more >
Pandas : TypeError: Only valid with DatetimeIndex ... - YouTube
Pandas : TypeError : Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' and I can't figure out ...
Read more >
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or ...
Coding example for the question TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' and I can't ...
Read more >
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or ...
typeerror: only valid with datetimeindex, timedeltaindex or periodindex, but got an instance of 'index' resample. Add Answer | View In TPC Matrix.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found