Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Databases and Redis · Comparison

Int64Index import fails after pandas moved to 2.0 or later

Short answer

ImportError: cannot import name 'Int64Index' from 'pandas' means pandas 2.0 or later is installed, and that release removed Int64Index, UInt64Index and Float64Index. In your own code use pd.Index(values, dtype="int64") and test index types by dtype. When the import sits inside xgboost older than 1.6 or another library, upgrade that library. Holds for pandas 3.1.

  • Applies to pandas 3.1, pandas 2.0 and later, xgboost 3.4, statsmodels 0.15, Python 3.11 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

The pandas 2.0.0 release notes explain the change: before 2.0, Index could only hold int64, uint64 and float64 data, each with its own class (Int64Index, UInt64Index, Float64Index). From 2.0 the ordinary Index holds any numpy numeric dtype, and the three special classes were removed after being deprecated in 1.4. Any import of them now fails with the ImportError in the title, in pandas 2 and in later releases.

Whether the fix is in your code or in a package you installed depends on which file the traceback names, so start there.

At a glance

Where the failing import lives and what to change
Traceback points toMeaningAction
A file in your repositoryYour code asks for a deleted classUse pd.Index with a dtype; test types with pandas.api.types
xgboost/compat.pyxgboost older than 1.6 on pandas 2 or laterUpgrade xgboost in that environment
Another package under site-packagesA library last released before pandas 2Upgrade it; if no fixed release exists, isolate it with pandas below 2

Read the line above the ImportError

The traceback names the file that holds the import. If the path is inside your project, the first fix applies. If it is under site-packages, the folder names the library.

Show the file that imports the class

python your_script.py 2>&1 | grep -B2 "Int64Index"

Replace the classes in your own code

Create an index with pd.Index(values, dtype=...). For checks, avoid comparing to class names, which pandas may retire. The pandas.api.types helpers test a dtype family instead. This matters because pandas 2 keeps the requested width, so an index built with int32 stays int32 where 1.x widened it to int64; a test for the exact string int64 can fail even when the index is perfectly integer.

Old calls and their replacements

import pandas as pd
from pandas.api.types import is_integer_dtype

# before: idx = pd.Int64Index([1, 2, 3])
idx = pd.Index([1, 2, 3], dtype="int64")

# before: if isinstance(df.index, pd.Int64Index):
if is_integer_dtype(df.index):
    ...
RemovedUse instead
pd.Int64Index(data)pd.Index(data, dtype="int64")
pd.UInt64Index(data)pd.Index(data, dtype="uint64")
pd.Float64Index(data)pd.Index(data, dtype="float64")
isinstance(idx, pd.Int64Index)is_integer_dtype(idx)

xgboost older than 1.6

xgboost 1.5 imported Int64Index in its compat.py. The xgboost maintainers reported that the class was removed from its code for 1.6.0, so any newer release imports cleanly on pandas 2. Check the installed version and upgrade it in the same environment.

Check, then upgrade

python -m pip show xgboost | grep Version
python -m pip install --upgrade xgboost

Other libraries from before pandas 2

Any package frozen before 2.0 may carry the same line. For example, statsmodels 0.13.1 imported the class under the alias NumericIndex. Search the installed packages to find every file that still mentions it, upgrade each package, and search again.

Find leftovers in the environment

grep -rl "Int64Index" "$(python -c 'import sysconfig; print(sysconfig.get_path("purelib"))')" --include=*.py

If one package has no fixed release, keep it in its own virtual environment with pandas<2, and note that pandas 1.x wheels stop at older Python versions.

Check that nothing else shifted

  1. Run the script again; the import error should be gone.
  2. Print df.index.dtype for the frames you care about and compare with a run on the old pandas.
  3. Look at code that joins or merges on integer indexes, since the width of the index can now differ between frames.

Which to pick

  • pandas 2.0 removed Int64Index, UInt64Index and Float64Index.
  • In your code: pd.Index(..., dtype=...) plus dtype helpers.
  • In packages: upgrade; search the whole environment, not just your repo.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.