How to convert Decimal128 to decimal in pandas dataframe











up vote
0
down vote

favorite
1












I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').



What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?



There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?



Edit, I've tried:



testdf =  my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)

testdf[testdf["MyCol"] > 80].head()


but I get:



TypeError: '>' not supported between instances of 'Decimal128' and 'int'


Converting a single column using .astype(str).astype(float) works.










share|improve this question
























  • I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
    – Mohit Motwani
    2 days ago










  • they're all instances of 'object', rather than str or Decimal128.
    – Rich
    2 days ago










  • Can you show how your dataframe looks?
    – Mohit Motwani
    2 days ago










  • looks like most are correct, apart from strings are coming in as dtype object
    – Rich
    2 days ago






  • 1




    Think i'll have to do it column by column rather than automating it. thanks for your help
    – Rich
    2 days ago















up vote
0
down vote

favorite
1












I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').



What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?



There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?



Edit, I've tried:



testdf =  my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)

testdf[testdf["MyCol"] > 80].head()


but I get:



TypeError: '>' not supported between instances of 'Decimal128' and 'int'


Converting a single column using .astype(str).astype(float) works.










share|improve this question
























  • I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
    – Mohit Motwani
    2 days ago










  • they're all instances of 'object', rather than str or Decimal128.
    – Rich
    2 days ago










  • Can you show how your dataframe looks?
    – Mohit Motwani
    2 days ago










  • looks like most are correct, apart from strings are coming in as dtype object
    – Rich
    2 days ago






  • 1




    Think i'll have to do it column by column rather than automating it. thanks for your help
    – Rich
    2 days ago













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').



What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?



There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?



Edit, I've tried:



testdf =  my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)

testdf[testdf["MyCol"] > 80].head()


but I get:



TypeError: '>' not supported between instances of 'Decimal128' and 'int'


Converting a single column using .astype(str).astype(float) works.










share|improve this question















I have a dataframe with many (But not all) Decimal128 columns (taken from a mongodb collection). I can't perform any math or comparisons on them (e.g. '<' not supported between instances of 'Decimal128' and 'float').



What is the quickest/easiest way to convert all these to float or some simpler built-in type that i can work with?



There is the Decimal128 to_decimal() method, and pandas astype(), but how can I do it for all (the decimal128) columns in one step/helper method?



Edit, I've tried:



testdf =  my_df.apply(lambda x: x.astype(str).astype(float) if isinstance(x, Decimal128) else x)

testdf[testdf["MyCol"] > 80].head()


but I get:



TypeError: '>' not supported between instances of 'Decimal128' and 'int'


Converting a single column using .astype(str).astype(float) works.







python pandas numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked Nov 17 at 13:30









Rich

2,29911218




2,29911218












  • I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
    – Mohit Motwani
    2 days ago










  • they're all instances of 'object', rather than str or Decimal128.
    – Rich
    2 days ago










  • Can you show how your dataframe looks?
    – Mohit Motwani
    2 days ago










  • looks like most are correct, apart from strings are coming in as dtype object
    – Rich
    2 days ago






  • 1




    Think i'll have to do it column by column rather than automating it. thanks for your help
    – Rich
    2 days ago


















  • I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
    – Mohit Motwani
    2 days ago










  • they're all instances of 'object', rather than str or Decimal128.
    – Rich
    2 days ago










  • Can you show how your dataframe looks?
    – Mohit Motwani
    2 days ago










  • looks like most are correct, apart from strings are coming in as dtype object
    – Rich
    2 days ago






  • 1




    Think i'll have to do it column by column rather than automating it. thanks for your help
    – Rich
    2 days ago
















I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago




I think the isinstance method is returning false and hence the columns are not being converted to float. I think if you use the the exact class of Decimal128, this method will work.
– Mohit Motwani
2 days ago












they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago




they're all instances of 'object', rather than str or Decimal128.
– Rich
2 days ago












Can you show how your dataframe looks?
– Mohit Motwani
2 days ago




Can you show how your dataframe looks?
– Mohit Motwani
2 days ago












looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago




looks like most are correct, apart from strings are coming in as dtype object
– Rich
2 days ago




1




1




Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago




Think i'll have to do it column by column rather than automating it. thanks for your help
– Rich
2 days ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Casting full DataFrame.



df = df.astype(str).astype(float)


For single column. IDs is the name of the column.



df["IDs"] = df.IDs.astype(str).astype(float)


Test implementation



from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)


Output:



[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]

D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143





share|improve this answer























  • Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
    – Rich
    Nov 17 at 15:51










  • @Rich Updated the answer. Can you please check now and let me know if that works. :)
    – Chirag
    Nov 17 at 16:23










  • casting through string seems to work. Doesn't look very efficient but hey-ho
    – Rich
    Nov 17 at 17:06


















up vote
1
down vote













Just use:



df = df.astype(float)


You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.



df = df.applymap(float)


I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?



df =  df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)


It will check if a column is of type Decimal128 and then convert it to float.






share|improve this answer























  • Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
    – Rich
    Nov 17 at 15:26










  • Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
    – Mohit Motwani
    Nov 17 at 19:45










  • Thanks, but no luck, I've updated my question
    – Rich
    2 days ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351707%2fhow-to-convert-decimal128-to-decimal-in-pandas-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Casting full DataFrame.



df = df.astype(str).astype(float)


For single column. IDs is the name of the column.



df["IDs"] = df.IDs.astype(str).astype(float)


Test implementation



from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)


Output:



[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]

D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143





share|improve this answer























  • Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
    – Rich
    Nov 17 at 15:51










  • @Rich Updated the answer. Can you please check now and let me know if that works. :)
    – Chirag
    Nov 17 at 16:23










  • casting through string seems to work. Doesn't look very efficient but hey-ho
    – Rich
    Nov 17 at 17:06















up vote
1
down vote



accepted










Casting full DataFrame.



df = df.astype(str).astype(float)


For single column. IDs is the name of the column.



df["IDs"] = df.IDs.astype(str).astype(float)


Test implementation



from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)


Output:



[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]

D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143





share|improve this answer























  • Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
    – Rich
    Nov 17 at 15:51










  • @Rich Updated the answer. Can you please check now and let me know if that works. :)
    – Chirag
    Nov 17 at 16:23










  • casting through string seems to work. Doesn't look very efficient but hey-ho
    – Rich
    Nov 17 at 17:06













up vote
1
down vote



accepted







up vote
1
down vote



accepted






Casting full DataFrame.



df = df.astype(str).astype(float)


For single column. IDs is the name of the column.



df["IDs"] = df.IDs.astype(str).astype(float)


Test implementation



from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)


Output:



[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]

D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143





share|improve this answer














Casting full DataFrame.



df = df.astype(str).astype(float)


For single column. IDs is the name of the column.



df["IDs"] = df.IDs.astype(str).astype(float)


Test implementation



from pprint import pprint
import bson
df = pd.DataFrame()
y =
for i in range(1,6):
i = i *2/3.5
y.append(bson.decimal128.Decimal128(str(i)))
pprint(y)
df["D128"] = y
df["D128"] = df.D128.astype(str).astype(float)
print("n", df)


Output:



[Decimal128('0.5714285714285714'),
Decimal128('1.1428571428571428'),
Decimal128('1.7142857142857142'),
Decimal128('2.2857142857142856'),
Decimal128('2.857142857142857')]

D128
0 0.571429
1 1.142857
2 1.714286
3 2.285714
4 2.857143






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 at 16:37

























answered Nov 17 at 13:38









Chirag

571210




571210












  • Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
    – Rich
    Nov 17 at 15:51










  • @Rich Updated the answer. Can you please check now and let me know if that works. :)
    – Chirag
    Nov 17 at 16:23










  • casting through string seems to work. Doesn't look very efficient but hey-ho
    – Rich
    Nov 17 at 17:06


















  • Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
    – Rich
    Nov 17 at 15:51










  • @Rich Updated the answer. Can you please check now and let me know if that works. :)
    – Chirag
    Nov 17 at 16:23










  • casting through string seems to work. Doesn't look very efficient but hey-ho
    – Rich
    Nov 17 at 17:06
















Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51




Gives "TypeError: float() argument must be a string or a number, not 'Decimal128'"
– Rich
Nov 17 at 15:51












@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23




@Rich Updated the answer. Can you please check now and let me know if that works. :)
– Chirag
Nov 17 at 16:23












casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06




casting through string seems to work. Doesn't look very efficient but hey-ho
– Rich
Nov 17 at 17:06












up vote
1
down vote













Just use:



df = df.astype(float)


You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.



df = df.applymap(float)


I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?



df =  df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)


It will check if a column is of type Decimal128 and then convert it to float.






share|improve this answer























  • Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
    – Rich
    Nov 17 at 15:26










  • Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
    – Mohit Motwani
    Nov 17 at 19:45










  • Thanks, but no luck, I've updated my question
    – Rich
    2 days ago















up vote
1
down vote













Just use:



df = df.astype(float)


You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.



df = df.applymap(float)


I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?



df =  df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)


It will check if a column is of type Decimal128 and then convert it to float.






share|improve this answer























  • Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
    – Rich
    Nov 17 at 15:26










  • Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
    – Mohit Motwani
    Nov 17 at 19:45










  • Thanks, but no luck, I've updated my question
    – Rich
    2 days ago













up vote
1
down vote










up vote
1
down vote









Just use:



df = df.astype(float)


You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.



df = df.applymap(float)


I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?



df =  df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)


It will check if a column is of type Decimal128 and then convert it to float.






share|improve this answer














Just use:



df = df.astype(float)


You can also use apply or applymap(applying element wise operations), although these are inefficient compared to previous method.



df = df.applymap(float)


I can't reproduce a Decimal128 number in my system. Can you please check if the next line works for you?



df =  df.apply(lambda x: x.astype(float) if isinstance(x, bson.decimal.Decimal128) else x)


It will check if a column is of type Decimal128 and then convert it to float.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 at 19:44

























answered Nov 17 at 13:38









Mohit Motwani

612319




612319












  • Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
    – Rich
    Nov 17 at 15:26










  • Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
    – Mohit Motwani
    Nov 17 at 19:45










  • Thanks, but no luck, I've updated my question
    – Rich
    2 days ago


















  • Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
    – Rich
    Nov 17 at 15:26










  • Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
    – Mohit Motwani
    Nov 17 at 19:45










  • Thanks, but no luck, I've updated my question
    – Rich
    2 days ago
















Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26




Can you do this solely for the decimal128 columns? e.g. foreach column if decimal128 then convert, else leave it alone?
– Rich
Nov 17 at 15:26












Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45




Hi @Rich I've edited my answer. Can you please have a look and tell me if it works for you?
– Mohit Motwani
Nov 17 at 19:45












Thanks, but no luck, I've updated my question
– Rich
2 days ago




Thanks, but no luck, I've updated my question
– Rich
2 days ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351707%2fhow-to-convert-decimal128-to-decimal-in-pandas-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno