## USE OF DTYPE IN SERIES

dtype parameter is used to provide the data type to the Series elements. Since a single series can have a single data type, we can assign our own data type using dtype parameter.

### Series Method Prototype

In the previous posts we have seen data Parameter and index Parameter, in this Post we will discuss dtype Parameter. By default, the dtype will be inferred by Series elements.

<Series Object> =  pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

dtype can be:
• If not specified, this will be inferred from data.
• Any NumPy data type

### a)  dtype - inferred from data

When we do not provide the dtype at the time of series creation it will automatically be taken by python from the data of Series. Check the following examples:

#### (i) Creating Series with NumPy array

In the following example, we are creating the series using the NumPy array. The data type of Series is coming as int32. This is taken by the python from the data because all the data are of integer type, hence "int32" was given as data type of Series.

```import numpy as np
import pandas as pd
arr = np.array([1,2,3,4])
print(arr)
s = pd.Series(arr)
print(s)
print(type(s))

'''
Output:
-------
[1 2 3 4]

0    1
1    2
2    3
3    4
dtype: int32

<class 'pandas.core.series.Series'>
'''
```

#### (ii) Using List

When we use list for the creation of Series, it takes the index of Series same as the index of the list. As shown in the example the list myList is shaving Three values, these values can be of any type. Here the list contains the string and integer both, hence the python has given the dtype as "object"
```import pandas as pd
myList = ['A','B',2]
s = pd.Series(myList)
print(s)
print(type(s))

'''
Output:
-------
0    A
1    B
2    2
dtype: object

<class 'pandas.core.series.Series'>
'''
```

#### (iii) Using Dictionary

When a dictionary is used for Series creation, the values of that dictionary is used as data of Series and Keys are used as data labels (index) of Series. In this case, all the Series elements are integer, hence the data type is coming "int64".
```import pandas as pd
d = {'A':1, 'B':2, 'C':3}
print(d)
s = pd.Series(d)
print(s)
print(type(s))

'''
Output:
------
{'A': 1, 'B': 2, 'C': 3}

A    1
B    2
C    3
dtype: int64

<class 'pandas.core.series.Series'>
'''
```

### b) Any NumPy Data Type

At the time of Series Creation, we can provide our own data type using dtype Parameter. Following is the list of NumPy Data Type.

Data Type Description
bool_ Boolean (True or False) stored as a byte
int8 Byte (-128 to 127)
int16 Integer (-32768 to 32767)
int32 Integer (-2.15E-9 to 2.15E+9)
int64 Integer (-9.22E-18 to 9.22E+18)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4.29E+9)
uint64 Unsigned integer (O to 1.84E+19)
float16 Half precision signed float
float32 Single precision signed float
float64 Double precision signed float
complex64 Complex number: two 32-bit floats (real and imaginary components)
complex128 Complex number: two 64-bit floats (real and imaginary components)

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```import pandas as pd import numpy as np s = pd.Series(data = [1,2,3,4], dtype = np.int16) print(s) ''' #Output: 0 1 1 2 2 3 3 4 dtype: int16 ''' s = pd.Series(data = [1,2,3,4], dtype = np.float32) print(s) ''' #Output: 0 1.0 1 2.0 2 3.0 3 4.0 dtype: float32 ''' ```

In the above example, you can see that we have provided "np.int16" and "np.float32" respectively for the same data [1,2,3,4].