Python | Pandas Index.copy()
Last Updated :
12 Jan, 2022
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages.
Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas
Index.copy()
function make a copy of this object. The function also sets the name and dtype attribute of the new object as that of original object. If we wish to have a different datatype for the new object then we can do that by setting the dtype attribute of the function.
Syntax: Index.copy(name=None, deep=False, dtype=None, **kwargs)
Parameters :
name : string, optional
deep : boolean, default False
dtype : numpy dtype or pandas type
Returns : copy : Index
Note : In most cases, there should be no functional difference from using deep, but if deep is passed it will attempt to deepcopy.
Example #1: Use
Index.copy()
function to copy the Index value to a new object and change the datatype of new object to 'int64'
Python3
# importing pandas as pd
import pandas as pd
# Creating the Index
idx = pd.Index([17.3, 69.221, 33.1, 15.5, 19.3, 74.8, 10, 5.5])
# Print the Index
idx
Output :

Let's create a copy of the object having 'int64' data type.
Python3
# Change the data type of newly
# created object to 'int64'
idx.copy(dtype ='int64')
Output :

As we can see in the output, the function has returned a copy of the original Index with 'int64' dtype.
Example #2: Use
Index.copy()
function to make a copy of the original object. Also set the name attribute of the new object and convert the string dtype into 'datetime' type.
Python3
# importing pandas as pd
import pandas as pd
# Creating the Index
idx = pd.Index(['2015-10-31', '2015-12-02', '2016-01-03',
'2016-02-08', '2017-05-05'])
# Print the Index
idx
Output :

Let's make a copy of the original object.
Python3
# to make copy and set data type in the datetime format.
idx_copy = idx.copy(dtype ='datetime64')
# Print the newly created object
idx_copy
Output :

As we can see in the output, the new object has the data in datetime format and its name attribute has also been set.
Similar Reads
Python | Pandas Index.asof() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.asof() function returns return the label from the index, or, if not prese
2 min read
Python | Pandas Index.dtype 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 Index.dtype attribute return the data type (dtype) of the underlying data of the given Index object. Syntax: Index.dtype Parameter : None Re
2 min read
Python | Pandas Index.append() Index.append() method in Pandas is used to concatenate or append one Index object with another Index or a list/tuple of Index objects, returning a new Index object. It does not modify the original Index. Example:Pythonimport pandas as pd idx1 = pd.Index([1, 2, 3]) idx2 = pd.Index([4, 5]) res = idx1.
2 min read
Python | Pandas Index.astype() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.astype() function create an Index with values cast to dtypes. The class o
2 min read
Python | Pandas Index.data 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 Index.data attribute return the data pointer of the underlying data of the given Index object. Syntax: Index.data Parameter : None Returns :
2 min read