Rpy2 conversion of categorical data containing nulls to R factors
I have a pandas dataframe with a categorical column containing NaN values, e.g.:
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
g
0 A
1 B
2 C
3 NaN
dtype: category
Categories (3, object): [A, B, C]
In pandas NaN is not a category but you can have NaN values in your categorical data. I want to pass this dataframe through to R using %%R in a Jupyter notebook . The categorical column is successfully recognised by R as a factor, but the factor is malformed, presumably because of the Nan values:
%%R -i g
str(g)
Factor w/ 3 levels "A","B","C": 1 2 3 0
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
print(g)
Error in as.character.factor(x) : malformed factor
Is there any way to make sure that the factor is not malformed - e.g. to have an
NA factor level created automatically?
R: 3.5.1, rpy2: 2.9.4, Python - 3
r pandas rpy2 categorical-data factors
add a comment |
I have a pandas dataframe with a categorical column containing NaN values, e.g.:
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
g
0 A
1 B
2 C
3 NaN
dtype: category
Categories (3, object): [A, B, C]
In pandas NaN is not a category but you can have NaN values in your categorical data. I want to pass this dataframe through to R using %%R in a Jupyter notebook . The categorical column is successfully recognised by R as a factor, but the factor is malformed, presumably because of the Nan values:
%%R -i g
str(g)
Factor w/ 3 levels "A","B","C": 1 2 3 0
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
print(g)
Error in as.character.factor(x) : malformed factor
Is there any way to make sure that the factor is not malformed - e.g. to have an
NA factor level created automatically?
R: 3.5.1, rpy2: 2.9.4, Python - 3
r pandas rpy2 categorical-data factors
The other direction (NA in R factor converted to Python) also has an issue ( stackoverflow.com/questions/53236532/…). Can you open an issue on the rpy2 issue tracker ?
– lgautier
Nov 15 '18 at 14:11
Thanks @lgautier. I have opened an issue.
– panda
Nov 16 '18 at 0:21
@lgautier can you suggest a workaround until the issue is fixed?
– panda
Nov 16 '18 at 10:12
add a comment |
I have a pandas dataframe with a categorical column containing NaN values, e.g.:
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
g
0 A
1 B
2 C
3 NaN
dtype: category
Categories (3, object): [A, B, C]
In pandas NaN is not a category but you can have NaN values in your categorical data. I want to pass this dataframe through to R using %%R in a Jupyter notebook . The categorical column is successfully recognised by R as a factor, but the factor is malformed, presumably because of the Nan values:
%%R -i g
str(g)
Factor w/ 3 levels "A","B","C": 1 2 3 0
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
print(g)
Error in as.character.factor(x) : malformed factor
Is there any way to make sure that the factor is not malformed - e.g. to have an
NA factor level created automatically?
R: 3.5.1, rpy2: 2.9.4, Python - 3
r pandas rpy2 categorical-data factors
I have a pandas dataframe with a categorical column containing NaN values, e.g.:
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
g
0 A
1 B
2 C
3 NaN
dtype: category
Categories (3, object): [A, B, C]
In pandas NaN is not a category but you can have NaN values in your categorical data. I want to pass this dataframe through to R using %%R in a Jupyter notebook . The categorical column is successfully recognised by R as a factor, but the factor is malformed, presumably because of the Nan values:
%%R -i g
str(g)
Factor w/ 3 levels "A","B","C": 1 2 3 0
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
print(g)
Error in as.character.factor(x) : malformed factor
Is there any way to make sure that the factor is not malformed - e.g. to have an
NA factor level created automatically?
R: 3.5.1, rpy2: 2.9.4, Python - 3
r pandas rpy2 categorical-data factors
r pandas rpy2 categorical-data factors
asked Nov 15 '18 at 4:02
pandapanda
8011
8011
The other direction (NA in R factor converted to Python) also has an issue ( stackoverflow.com/questions/53236532/…). Can you open an issue on the rpy2 issue tracker ?
– lgautier
Nov 15 '18 at 14:11
Thanks @lgautier. I have opened an issue.
– panda
Nov 16 '18 at 0:21
@lgautier can you suggest a workaround until the issue is fixed?
– panda
Nov 16 '18 at 10:12
add a comment |
The other direction (NA in R factor converted to Python) also has an issue ( stackoverflow.com/questions/53236532/…). Can you open an issue on the rpy2 issue tracker ?
– lgautier
Nov 15 '18 at 14:11
Thanks @lgautier. I have opened an issue.
– panda
Nov 16 '18 at 0:21
@lgautier can you suggest a workaround until the issue is fixed?
– panda
Nov 16 '18 at 10:12
The other direction (NA in R factor converted to Python) also has an issue ( stackoverflow.com/questions/53236532/…). Can you open an issue on the rpy2 issue tracker ?
– lgautier
Nov 15 '18 at 14:11
The other direction (NA in R factor converted to Python) also has an issue ( stackoverflow.com/questions/53236532/…). Can you open an issue on the rpy2 issue tracker ?
– lgautier
Nov 15 '18 at 14:11
Thanks @lgautier. I have opened an issue.
– panda
Nov 16 '18 at 0:21
Thanks @lgautier. I have opened an issue.
– panda
Nov 16 '18 at 0:21
@lgautier can you suggest a workaround until the issue is fixed?
– panda
Nov 16 '18 at 10:12
@lgautier can you suggest a workaround until the issue is fixed?
– panda
Nov 16 '18 at 10:12
add a comment |
1 Answer
1
active
oldest
votes
At the time of writing this is a bug with rpy2's conversion of pandas categories, that is fixed and will be included in rpy2 starting with release 2.9.5: https://bitbucket.org/rpy2/rpy2/issues/493/rpy2-conversion-of-categorical-data
A workaround is rather trivial: don't use a NaN
in a pandas Category.
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
# Prepare alternative representation to pass it to R
g_r = g.replace(np.nan, 'Missing')
When converting it is now looking like:
%%R -i g_r
str(g_r)
Factor w/ 4 levels "A","B","C","Missing": 1 2 3 4
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
Translating back into an R NA is only a matter of dropping that added level:
%%R -i g_r
str(droplevels(g_r, exclude = "Missing"))
Factor w/ 3 levels "A","B","C": 1 2 3 NA
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312259%2frpy2-conversion-of-categorical-data-containing-nulls-to-r-factors%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
At the time of writing this is a bug with rpy2's conversion of pandas categories, that is fixed and will be included in rpy2 starting with release 2.9.5: https://bitbucket.org/rpy2/rpy2/issues/493/rpy2-conversion-of-categorical-data
A workaround is rather trivial: don't use a NaN
in a pandas Category.
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
# Prepare alternative representation to pass it to R
g_r = g.replace(np.nan, 'Missing')
When converting it is now looking like:
%%R -i g_r
str(g_r)
Factor w/ 4 levels "A","B","C","Missing": 1 2 3 4
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
Translating back into an R NA is only a matter of dropping that added level:
%%R -i g_r
str(droplevels(g_r, exclude = "Missing"))
Factor w/ 3 levels "A","B","C": 1 2 3 NA
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
add a comment |
At the time of writing this is a bug with rpy2's conversion of pandas categories, that is fixed and will be included in rpy2 starting with release 2.9.5: https://bitbucket.org/rpy2/rpy2/issues/493/rpy2-conversion-of-categorical-data
A workaround is rather trivial: don't use a NaN
in a pandas Category.
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
# Prepare alternative representation to pass it to R
g_r = g.replace(np.nan, 'Missing')
When converting it is now looking like:
%%R -i g_r
str(g_r)
Factor w/ 4 levels "A","B","C","Missing": 1 2 3 4
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
Translating back into an R NA is only a matter of dropping that added level:
%%R -i g_r
str(droplevels(g_r, exclude = "Missing"))
Factor w/ 3 levels "A","B","C": 1 2 3 NA
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
add a comment |
At the time of writing this is a bug with rpy2's conversion of pandas categories, that is fixed and will be included in rpy2 starting with release 2.9.5: https://bitbucket.org/rpy2/rpy2/issues/493/rpy2-conversion-of-categorical-data
A workaround is rather trivial: don't use a NaN
in a pandas Category.
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
# Prepare alternative representation to pass it to R
g_r = g.replace(np.nan, 'Missing')
When converting it is now looking like:
%%R -i g_r
str(g_r)
Factor w/ 4 levels "A","B","C","Missing": 1 2 3 4
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
Translating back into an R NA is only a matter of dropping that added level:
%%R -i g_r
str(droplevels(g_r, exclude = "Missing"))
Factor w/ 3 levels "A","B","C": 1 2 3 NA
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
At the time of writing this is a bug with rpy2's conversion of pandas categories, that is fixed and will be included in rpy2 starting with release 2.9.5: https://bitbucket.org/rpy2/rpy2/issues/493/rpy2-conversion-of-categorical-data
A workaround is rather trivial: don't use a NaN
in a pandas Category.
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
# Prepare alternative representation to pass it to R
g_r = g.replace(np.nan, 'Missing')
When converting it is now looking like:
%%R -i g_r
str(g_r)
Factor w/ 4 levels "A","B","C","Missing": 1 2 3 4
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
Translating back into an R NA is only a matter of dropping that added level:
%%R -i g_r
str(droplevels(g_r, exclude = "Missing"))
Factor w/ 3 levels "A","B","C": 1 2 3 NA
- attr(*, "names")= chr [1:4] "0" "1" "2" "3"
edited Nov 17 '18 at 21:48
answered Nov 17 '18 at 21:21
lgautierlgautier
8,9802138
8,9802138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312259%2frpy2-conversion-of-categorical-data-containing-nulls-to-r-factors%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
The other direction (NA in R factor converted to Python) also has an issue ( stackoverflow.com/questions/53236532/…). Can you open an issue on the rpy2 issue tracker ?
– lgautier
Nov 15 '18 at 14:11
Thanks @lgautier. I have opened an issue.
– panda
Nov 16 '18 at 0:21
@lgautier can you suggest a workaround until the issue is fixed?
– panda
Nov 16 '18 at 10:12