Index.copy()
Pandas Index.copy()
makes a copy of this object. The function also sets the name and dtype of the new object as an attribute of the original object. If we want to have a different data type for the new object, we can do so 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 typeReturns: copy: Index
Note: in most cases there should be no functional difference from using deep, but if deep is passed it will try to deep copy.
Example # 1: Use Index.copy ()
to copy the Index value into a new object and change the data type of the new object to int64.
|
Output:
Let’s create a copy of an int64 object.
|
Output:
As we see in the output, the function returned a copy of the original index with the int64 type.
Example # 2: Use Index.copy ()
to make a copy of the original object. Also set the name attribute of the new object and convert the string d to datetime.
|
Output:
Let’s make a copy of the original object.
|
Output:
As we can see in the output, the new object has data in datatime format, and its name attribute has also been set.