Python TypeError: Cannot do slice stop value indexing on <class 'pandas.core.index.Int64Index'>

Abhishek Jaiswal

Am getting a Type Error, while trying to filter some past data using python and pandas. Here's the error

TypeError: cannot do slice stop value indexing on < class 'pandas.core.index.Int64Index'> with these indexers [327.0] of < type 'float'>

Code

# 65% of training data
ratio = 0.65
train_data_df = df_replace[:round(dataset_length*ratio)]  
test_data_df = df_replace[-(1-round(dataset_length*ratio)):]  

# Create Respected CSV
train_data_df.to_csv('Train.csv',index=False)
test_data_df.to_csv('Test.csv',index=False)

Additional Info

The code is working upto creating a new CSV file India_in_Tests_Filter.csv that has more than 450 rows and 3 columns as follows:

Result Toss Bat

Lost   won  1st 
Won    won  2nd

While India_in_Tests.csv have more than 450 rows and 7 columns.

So folks, any thoughts on that?

piRSquared

consider df

df = pd.DataFrame(range(10), list(range(320, 330)))

then slice it with

df[:327.0]
TypeError: cannot do slice indexing on <class 'pandas.indexes.numeric.Int64Index'>
with these indexers [327.0] of <type 'float'>

your round function is returning a float. Make it an int instead

df[:int(327.0)]

enter image description here

what your code should look like

train_data_df = df_replace[:int(dataset_length*ratio)]  
test_data_df = df_replace[-(1-int(dataset_length*ratio)):]  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python pandas slice dataframe by multiple index ranges

pandas: Boolean indexing with multi index

Python DatetimeIndex Error - TypeError: ("cannot do label indexing on <class 'pandas.tseries.index.DatetimeIndex'

Pandas - Indexing by not in index

How do I slice a pandas time series on dates not in the index?

Python Pandas: cannot do slice indexing

cannot do slice indexing on <class 'pandas.indexes.range.RangeIndex'> with these indexers

TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers

TypeError: unhashable type: 'Int64Index'

Slice a Dataframe Index per year - Python - Pandas

Python Pandas filtering; TypeError: cannot convert the series to <class 'int'>

Subtracting indexes - TypeError: cannot perform __sub__ with this index type: <class 'pandas.core.indexes.base.Index'>

Python TypeError: cannot do positional indexing on <class 'pandas.core.indexes.numeric.Int64Index'>

Python Pandas Join with Index value

Pandas convert from Int64Index to RangeIndex

Pandas indexing and what .index does

Featuretools TypeError: unhashable type: 'Int64Index'

Use strings as slice indices in Python ? (TypeError: slice indices must be integers or None or have an __index__ method)

Pandas Int64 .loc cannot do slice indexing?

Ataining an element of an array in numpy TypeError: cannot do slice indexing

TypeError: 'Int64Index' object is not callable

Select date column by locations: #IndexingError: Too many indexers AND TypeError: cannot do slice indexing

Cannot get right slice bound for non-unique label when indexing data frame with python-pandas

pandas feeding slice as pandas.core.indexes.numeric.Int64Index getting TypeError: slice indices must be integers

Error Message: cannot do slice indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [[ 2]] of <class 'numpy.ndarray'>

TypeError: cannot do slice indexing on Int64Index with these indexers [2018-12-01] of type str

How to do positional indexing on a pandas series with multi-index

Tuple Indexing, Combine Slice and Index

TypeError: cannot do positional indexing on Int64Index with these indexers [Int64Index([5], dtype='int64')] of type Int64Index