You can do it that way:

# for Python 2
df.index = df.index.map(unicode) 

# for Python 3 (the unicode type does not exist and is replaced by str)
df.index = df.index.map(str)

As for why you would proceed differently from when you'd convert from int to float, that's a peculiarity of numpy (the library on which pandas is based).

Every numpy array has a dtype, which is basically the machine type of its elements : in that manner, numpy deals directly with native types, not with Python objects, which explains how it is so fast. So when you are changing the dtype from int64 to float64, numpy will cast each element in the C code.

There's also a special dtype : object, that will basically provide a pointer toward a Python object.

If you want strings, you thus have to use the object dtype. But using .astype(object) would not give you the answer you were looking for : it would instead create an index with object dtype, but put Python float objects inside.

Here, by using map, we convert the index to strings with the appropriate function: numpy gets the string objects and understand that the index has to have an object dtype, because that's the only dtype that can accomodate strings.

Answer from Salomé on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.astype.html
pandas.Index.astype — pandas 3.0.3 documentation - PyData |
>>> idx = pd.Index([1, 2, 3]) >>> idx Index([1, 2, 3], dtype='int64') >>> idx.astype("float") Index([1.0, 2.0, 3.0], dtype='float64')
🌐
Skytowner
skytowner.com › explore › changing_the_type_of_a_dataframes_index_in_pandas
Changing the type of a DataFrame's index in Pandas
To change the type of a DataFrame's index in Pandas, use the DataFrame.index.astype(~) method.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.html
pandas.Index — pandas 3.0.3 documentation
This could be a Python list, a NumPy array, or a pandas Series. dtypestr, numpy.dtype, or ExtensionDtype, optional · Data type for the output Index. If not specified, this will be inferred from data. See the user guide for more usages. ... Whether to copy input data, only relevant for array, Series, and Index inputs (for other input, e.g. a list, a new array is created anyway). Defaults to True for array input and False for Index/Series. Set to False to avoid copying array input at your own risk (if you know the input data won’t be modified elsewhere).
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.set_index.html
pandas.DataFrame.set_index — pandas 3.0.3 documentation
Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length).
🌐
GitHub
github.com › pandas-dev › pandas › issues › 30517
DataFrame.set_index() may not preserve dtype · Issue #30517 · pandas-dev/pandas
December 27, 2019 - In [1]: import pandas as pd In [2]: pd.__version__ Out[2]: '0.25.3' In [3]: df = pd.DataFrame({'mixed' : [1, 2, 'abc', 'def'], 'ints': [100, 200, 3 ...: 00, 400]}) In [4]: df Out[4]: mixed ints 0 1 100 1 2 200 2 abc 300 3 def 400 In [5]: df.dtypes Out[5]: mixed object ints int64 dtype: object In [6]: df.query('ints < 300').set_index('mixed').index Out[6]: Int64Index([1, 2], dtype='int64', name='mixed') In [7]: df.set_index('mixed').query('ints < 300').index Out[7]: Index([1, 2], dtype='object', name='mixed')
Author   pandas-dev
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-index-dtype
Python | Pandas Index.dtype - GeeksforGeeks
April 10, 2024 - Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Index.astype.html
pandas.Index.astype — pandas 2.2.1 documentation - PyData |
>>> idx = pd.Index([1, 2, 3]) >>> idx Index([1, 2, 3], dtype='int64') >>> idx.astype("float") Index([1.0, 2.0, 3.0], dtype='float64')
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › python-pandas-create-an-index-with-values-cast-to-dtypes
Python Pandas - Create an Index with values cast to dtypes
October 13, 2021 - import pandas as pd # Creating Pandas index index = pd.Index([50.4, 10.2, 70.5, 110.5, 90.8, 50.6]) # Display the Pandas index print("Pandas Index...\n",index) # Return the number of elements in the Index print("\nNumber of elements in the index...\n",index.size) # Return the dtype of the data print("\nThe dtype object...\n",index.dtype) # convert datatype to int64 print("\nIndex object after converting type...\n",index.astype('int64'))
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.dtype.html
pandas.Index.dtype — pandas 3.0.3 documentation - PyData |
Return the dtype object of the underlying data · Return a string of the type inferred from the values
🌐
GeeksforGeeks
geeksforgeeks.org › python-pandas-index-astype
Python | Pandas Index.astype() | GeeksforGeeks
December 16, 2018 - It is the basic object which stores the axis labels for all pandas objects. Pandas Index.dtype_str attribute return the data type (dtype) of the underlying data of the given Index object as a string.
🌐
Pandas
pandas.pydata.org › docs › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.3 documentation
In [75]: s = pd.Series(range(10), index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']) In [76]: s.loc['b':'i':2] # Start at 'b' (position 1), stop at 'i' (position 8), step 2 positions → 'b', 'd', 'f', 'h' Out[76]: b 1 d 3 f 5 h 7 dtype: int64 In [77]: s.loc[['b', 'd', 'f', 'h']] # explicit label selection Out[77]: b 1 d 3 f 5 h 7 dtype: int64 · In both cases, start and stop determine the label boundaries (inclusive), while step skips positions within that range, regardless of the index type. pandas provides a suite of methods in order to get purely integer based indexing.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.index.html
pandas.DataFrame.index — pandas 3.0.3 documentation
>>> df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'], ... 'Age': [25, 30, 35], ... 'Location': ['Seattle', 'New York', 'Kona']}, ... index=([10, 20, 30])) >>> df.index Index([10, 20, 30], dtype='int64') In this example, we create a DataFrame with 3 rows and 3 columns, including Name, Age, and Location information. We set the index labels to be the integers 10, 20, and 30.
🌐
GitHub
github.com › dask › dask › issues › 9118
Cannot set index to a column with a `period` dtype · Issue #9118 · dask/dask
May 23, 2022 - What happened: Dask.dataframe errors when trying to call dd.set_index with a period dtype. ... import dask.dataframe as dd import pandas df = pandas.DataFrame({ "a": range(10), "b": pandas.period_range(start="2022-05-23", periods=10, freq="1D") }) dd.from_pandas(df, npartitions=2).set_index("b")
Author   dask
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Set a column as the DataFrame index with set_index() | note.nkmk.me
January 26, 2024 - source: pandas_set_index.py · By default, if there are duplicates in the new index, they are used as is. Setting verify_integrity=True results in an error if duplicates exist. print(df.set_index('state')) # name age point # state # NY Alice 24 64 # CA Bob 42 92 # CA Charlie 18 70 # TX Dave 68 70 # CA Ellen 24 88 # NY Frank 30 57 # print(df.set_index('state', verify_integrity=True)) # ValueError: Index has duplicate keys: Index(['CA', 'NY'], dtype='object', name='state') source: pandas_set_index.py ·
🌐
Appdividend
appdividend.com › 2019 › 01 › 26 › pandas-set-index-example-python-set_index-tutorial
Pandas DataFrame set_index(): Setting an Index Column
September 23, 2025 - import pandas as pd df = pd.DataFrame( { 'ID': [101, 102, 103, 104, 105, 106, 107], 'Team_Name': ["Lakers", "Patriots", "Yankees", "Lakers", "Red Sox", "Warriors", "Patriots"], 'Wins': [12, 10, 14, 12, 13, 15, 10] } ) multi_indexes_df = df.set_index(['ID', 'Team_Name']) print(multi_indexes_df.loc[(104, 'Lakers')]) # Output: # Wins 12 # Name: (104, Lakers), dtype: int64