Anand
Anand

Reputation: 143

Python3 - Pandas Resample function

This is my code:

import pandas as pd
data = pd.read_csv("temp.data",sep=';')

data['Date'] = pd.to_datetime(data['Date']+' '+data['Time'])
del data['Time']
data.rename(columns={'Date':'TimeStamp'}, inplace=True)

data = data.reset_index()
data['TimeStamp'] = pd.to_datetime(data['TimeStamp'], format='%d/%m/%Y %H:%M:%S')
data['num'] = pd.to_numeric(data['num'])
data = data.resample('1M')

Error: "TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'". Sample data (Original data has 200 thousand rows, with no missing values):

17/12/2006;01:58:00;3.600
17/12/2006;01:59:00;2.098
17/12/2006;02:00:00;1.334
17/12/2006;02:01:00;4.362
17/12/2006;02:02:00;1.258
17/12/2006;02:03:00;2.448
17/12/2006;02:04:00;5.426
17/12/2006;02:05:00;9.704

Upvotes: 0

Views: 334

Answers (1)

EFT
EFT

Reputation: 2369

As mentioned in the error, when trying to resample, you have a RangeIndex, which is just consecutive integers, while you need one that represents points in time.

Adding

data.set_index('TimeStamp', inplace=True)

before you resample will set your 'TimeStamp' column as the index. You can also do away with

data = data.reset_index()

unless you want an extra column labeled 'index' floating around.

Upvotes: 2

Related Questions