Pandas: Distinction between str and object types

Learn about the difference between the str and object data type in Python pandas. By Pranit Sharma Last updated : October 06, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Distinction between str and object types

Numpy always seems to make a distinction between str and object types. If we check the data type of str and object, it will return dtype('S') and dtype('O') respectively.

  • But, in the case of pandas, this distinction is not transparent. If we check the data type for str and object, it will return the same result for both the data types i.e., dtype('O').
  • The main reason behind this is that Numpy's string dtypes are not python strings. Therefore, pandas deliberately use native python strings, which require an object dtype.
  • Another difference between str and object data type is that str length does not go beyond a certain limit which we define at the time of defining a data type for numpy array. But, in the case of an object, there is no such limit.
  • Also, NumPy's strings are mutable, while Python strings are not.

Let us understand with the help of an example,

Python program to demonstrate the difference between the str and object data type

# Importing pandas package
import pandas as pd

# Creating a dataframe
df = pd.DataFrame({'A':[1,2,3,4,5,6,7,8,9]})

# Display the DataFrame
print("Original DataFrame:\n",df,"\n\n")

# Check dtype of string type of column A
print(df.A.astype(str).dtype,"\n")

# Check dtype of object type of column A
print(df.A.astype(object).dtype)

Output

The output of the above program is:

Example: Pandas: Distinction between str and object types

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.