• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Series.dtype() Function

Pandas Series.dytpe() function is used to return the data type of elements present in the Series. In Pandas, a Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, Python objects, etc.). When you create a Series, Pandas automatically infers the data type (dtype) based on the data you provide. You can also explicitly specify the dtype if needed.

In this article, I will explain the series.dtype() function and using this function how we can return the data type of the elements stored in the Series. It can be any valid NumPy data type such as int64, float64, bool, datetime64, timedelta64, object.

Key Points –

  • The dtype() function is used to retrieve the data type of the elements stored in a Pandas Series.
  • The dtype() function in Pandas Series provides a quick and efficient way to inspect the data type of the elements within the Series, aiding in data analysis and manipulation.
  • It returns a single data type representing the type of elements present in the Series.
  • The data type returned can be one of the NumPy data types such as int64, float64, bool, datetime64, timedelta64, object, etc
  • If the Series contains elements of mixed data types, the resulting dtype will be ‘object’, which essentially means it’s a generic data type that can store any Python object.

Series dtype() Function

The Series.dtype() function is a useful method in Pandas that helps to determine the data type of each element present in a Series. It can be applied directly to a Pandas Series object using dot(.) notation and does not require any parameters. The function returns the data type of the elements stored in the Series.

Syntax of the Series dtype() Function

Following is the syntax of the pandas series dtype() function.


# Syntax of series dtype()
Series.dtype

Return Value

It returns a single data type representing the type of elements present in the Series. It can be any valid NumPy data type such as int64, float64, bool, datetime64, timedelta64, object.

Get Integer dtype using Pandas Series dtype()

The integer dtype, denoted as int64, is a data type in Pandas used to represent integer values. It is capable of holding signed 64-bit integer values. This data type is commonly used for storing whole numbers without any decimal places. Let’s use the dtype() function on a given series and find out the type of elements you provide.


import pandas as pd

# Creating a Series with integer dtype
ser = pd.Series([5, 10, 15, 20, 25])
result = ser.dtype
print("Get the type of data in a Series:", result)

In the above example, the pd.Series() function creates a Series with integers [5, 10, 15, 20, 25]. Pandas returns the data type of the Series as int64 based on the provided data. This example yields the below output.

pandas series dtype

Get Float dtype

Alternatively, you can also use the dtype() function to get the float data type of the Series by providing the floating-point elements to the given Series. In Python, floating-point numbers represent decimal values.

When you create a Pandas Series with floating-point values, Pandas automatically infers the data type as float64, which signifies a 64-bit floating-point number. This data type is capable of storing decimal numbers with high precision.


import pandas as pd

# Creating a float Series
float_series  = pd.Series([3.4, 5.8, 6.1, 2.7])
result = float_series.dtype
print("Get the type of data in a Series:", result)

The above example float_series is a Pandas Series containing floating-point values [3.4, 5.8, 6.1, 2.7]. The data type of this Series is float64. This example yields the below output.

pandas series dtype

Get String/Object dtype

String/Object dtype in Pandas refers to a data type used to represent strings or other Python objects within a Pandas Series. It’s denoted as object when calling the dtype() function on a Pandas Series. This data type is versatile, as it allows for the storage of various Python objects, including strings, lists, dictionaries, and more.


import pandas as pd

# Creating a Series with string/object dtype
ser = pd.Series(['Spark', 'Pyspark', 'Pandas', 'Java', 'Hadoop'])
print(ser.dtype)

# Output:
# object

In the above example, the data type of the series ser is object. In Pandas, when a Series contains strings, the data type is typically represented as object.

Get Boolean dtype

Similarly, you can get a boolean data type(bool) when we use the dtype() function on the Series of boolean values. Boolean values refer to a data type used to represent boolean values within a Pandas Series. This data type is used to store binary data, typically representing True or False values.


import pandas as pd

# Creating a Boolean dtype Series
bool_series = pd.Series([True, False, True, True, False])
print(bool_series.dtype) 

# Output:
# bool

In the above example, bool_series is a Pandas Series containing boolean values. The dtype of this Series is bool, indicating that it stores boolean data.

Get Datetime dtype

Finally, you can get a datetime64[ns] data type when we apply the dtype() function directly to a Series of datetime objects. To create a Pandas Series with datetime dtype, you can provide datetime objects or use the to_datetime() function to convert strings to datetime objects.


import pandas as pd

# Creating a Series with datetime dtype
dates = ['2024-03-01', '2024-03-02', '2024-03-03', '2024-03-04', '2024-03-05']
ser = pd.Series(pd.to_datetime(dates))
print(ser.dtype)

# Output:
# datetime64[ns]

In the above example, we are creating a Series with datetime dtype using the pd.to_datetime() function to convert a list of strings representing dates into datetime objects. When you print the dtype of the Series.

Frequently Asked Questions on Pandas Series.dtype() Function

What is the purpose of the dtype() function in the Pandas Series?

The dtype() function in Pandas Series is used to determine the data type of the elements stored within the Series. It provides information about the type of data present in the Series, such as integer, float, string, datetime, etc.

How is the dtype() function used?

The dtype() function in Pandas Series is used to retrieve the data type of the elements stored within the Series. It is straightforward to use and can be applied directly to a Pandas Series object.

What does it mean if the dtype() function returns ‘object’?

If the dtype() function returns object, it indicates that the elements within the Series are of generic Python object type, often corresponding to strings or a mixture of different data types.

Can the dtype() function be used with a Series containing datetime values?

The dtype() function can be applied to Series containing datetime values. It will return datetime64[ns], indicating that the data type of the Series is datetime.

How does the dtype() function handle Series with mixed data types?

When a Series contains mixed data types, the dtype() function will typically return ‘object’ to accommodate the different types present in the Series.

Is it possible to change the data type of a Pandas Series using the dtype() function?

The dtype() function is used for retrieving the data type of a Series, not for changing it. To change the data type of a Series, you can use functions like astype() to convert the data type to the desired one.

Conclusion

In this article, you have learned about the pandas Series dtype() function and using this function how we can return the data type of the elements stored in the Series. It can be any valid NumPy data type such as int64, float64, bool, datetime64, timedelta64, object.

Happy Learning !!

Related Articles

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.