Python Forum
dtype in not working in mad() function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: dtype in not working in mad() function (/thread-19923.html)



dtype in not working in mad() function - ift38375 - Jul-20-2019

Given below is python pandas program using mad()function :
import pandas as pd
import numpy as np
diSales= { 2016:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
           2017:{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000},
           2018:{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500},
           2019:{'qtr1':61000}
         }
df = pd.DataFrame(diSales)
ks = df.mad(axis = 1)
print(ks)
My question is how can i change dtype of this pandas dataframe , by default it is showing float64 but i want to change it to int32 ? When i am trying command
ks = df.mad(axis = 1, dtype = int32)
then it is showing error Plz help


RE: dtype in not working in mad() function - ThomasL - Jul-20-2019

ks = df.mad(axis = 1).astype('int32')



RE: dtype in not working in mad() function - scidam - Jul-20-2019

Ability to use NaN values with columns of integer type is latest pandas feature (and still experimental).
As of Pandas 0.24.x you can do this, e.g.

import pandas as pd
import numpy as np
diSales= { 2016:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
           2017:{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000},
           2018:{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500},
           2019:{'qtr1':61000}
         }
df = pd.DataFrame(diSales, dtype=pd.Int32Dtype())
df.mad(axis=0)
df.info() returns integer dtype, but df.mad still returns float.

Output:
2016 6062.5 2017 6250.0 2018 2500.0 2019 0.0 dtype: float64



RE: dtype in not working in mad() function - ift38375 - Jul-21-2019

(Jul-20-2019, 08:48 AM)scidam Wrote: Ability to use NaN values with columns of integer type is latest pandas feature (and still experimental).
As of Pandas 0.24.x you can do this, e.g.

import pandas as pd
import numpy as np
diSales= { 2016:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
           2017:{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000},
           2018:{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500},
           2019:{'qtr1':61000}
         }
df = pd.DataFrame(diSales, dtype=pd.Int32Dtype())
df.mad(axis=0)
df.info() returns integer dtype, but df.mad still returns float.

Output:
2016 6062.5 2017 6250.0 2018 2500.0 2019 0.0 dtype: float64

is there any need of " import numpy as np" command in above program ?


RE: dtype in not working in mad() function - scidam - Jul-21-2019

(Jul-21-2019, 12:57 AM)ift38375 Wrote: is there any need of " import numpy as np" command in above program ?
In this case import numpy as np is redundant. However, Numpy is used frequently when doing some calculations and working with data. So, it is better to include that line in your script. Note, you can get access to numpy using pd.np.


RE: dtype in not working in mad() function - ift38375 - Jul-21-2019

(Jul-20-2019, 08:48 AM)scidam Wrote: Ability to use NaN values with columns of integer type is latest pandas feature (and still experimental).
As of Pandas 0.24.x you can do this, e.g.

import pandas as pd
import numpy as np
diSales= { 2016:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
           2017:{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000},
           2018:{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500},
           2019:{'qtr1':61000}
         }
df = pd.DataFrame(diSales, dtype=pd.Int32Dtype())
df.mad(axis=0)
df.info() returns integer dtype, but df.mad still returns float.

Output:
2016 6062.5 2017 6250.0 2018 2500.0 2019 0.0 dtype: float64

Still showing dtyle : float64 in output after using your code which have line: df = pd.DataFrame(diSales, dtype=pd.Int32Dtype()).why ? What is use of this line ?


RE: dtype in not working in mad() function - scidam - Jul-21-2019

(Jul-21-2019, 04:28 AM)ift38375 Wrote: df = pd.DataFrame(diSales, dtype=pd.Int32Dtype()).why ? What is use of this line ?
This is about new Pandas feature, see docs here.
if you call df.info(), you find that the data frame is of integer type; Nevertheless, .mad method, along with .mean method, still automatically convert it to a float type. So, the result of .mad execution has <float> type.


RE: dtype in not working in mad() function - ift38375 - Jul-22-2019

(Jul-21-2019, 11:05 PM)scidam Wrote:
(Jul-21-2019, 04:28 AM)ift38375 Wrote: df = pd.DataFrame(diSales, dtype=pd.Int32Dtype()).why ? What is use of this line ?
This is about new Pandas feature, see docs here.
if you call df.info(), you find that the data frame is of integer type; Nevertheless, .mad method, along with .mean method, still automatically convert it to a float type. So, the result of .mad execution has <float> type.

I think Df.info()is used for getting information about column-wise data type of dataframe but it is not used for convert whole float output into int. am i right ?


RE: dtype in not working in mad() function - scidam - Jul-22-2019

(Jul-22-2019, 12:44 AM)ift38375 Wrote: I think Df.info()is used for getting information about column-wise data type of dataframe but it is not used for convert whole float output into int. am i right ?
Yes, you are right. The .info method is just to get information about a data frame. If call it, you can see that the df created is of integer type, but .mad method still returns float.