Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flatten_df strips POSIXct class in top-level variables (related to #358 ?) #648

Closed
jemus42 opened this issue Feb 25, 2019 · 3 comments
Closed

Comments

@jemus42
Copy link

jemus42 commented Feb 25, 2019

When using flatten_df on a list, I expect the top-level variables in the output to have the same classes as before, yet this is not the case:

my_list <- list(a = lubridate::now(),
                b = list(f = 1, g = 2))

purrr::flatten_df(my_list)
#> # A tibble: 1 x 3
#>             a     f     g
#>         <dbl> <dbl> <dbl>
#> 1 1551127440.     1     2

# Expected output:
tibble::tibble(a = lubridate::now(), f = 1, g = 2)
#> # A tibble: 1 x 3
#>   a                       f     g
#>   <dttm>              <dbl> <dbl>
#> 1 2019-02-25 21:44:00     1     2

(Tested in purrr 0.3.0 and also the current dev version from GitHub)
Created on 2019-02-25 by the reprex package (v0.2.1)

I tried to find out if this is documented / intended behavior, but going by related issue #358, this is not intended, yet apparently fixed as the issue has been closed for a few months now and the thread indicates a successful PR.

Edit: Looking at the discussion of said PR, it looks like the implementation of flatten_* was not touched, so it makes sense that this issue is still present.

@lionel- lionel- added this to the vctrs milestone Feb 26, 2019
@hadley hadley closed this as completed Aug 27, 2022
@hadley hadley reopened this Aug 27, 2022
@hadley
Copy link
Member

hadley commented Aug 27, 2022

@jemus42 do you recall how you got to this point? It's a bit weird that f and g are wrapped in a list(), but a is not, and you want the outer name for a but the inner names for f and g. I'm fairly sceptical that purrr can resolve your problem with a single function call, and I'm leaning towards deprecating flatten_df() all together.

@jemus42
Copy link
Author

jemus42 commented Aug 27, 2022

Oh wow, it's been a while.

I don't recall the context specifically, but odds are I was trying to wrangle some API results that tend to come in uncomfortably nested JSON.
I often found myself trying to flatten that JSON output (parsed to a list of lists) without having to do too much manual restructuring via pluck since the output was often quite large and cumbersome, hoping that flatten_df would just magically get me 95% of the way to some usable data structure.
The issue here seemed like unintended behavior, so I thought that aspect could be worked around.

More specifically, I was probably wrangling trakt.tv API results (movies, tv shows, associated people, ...) in my hobby/procrastination package with some... adventurous "this has to be a tibble!" efforts I mildly regret.. Looking back, it's kind of a mess, but oh well.
That's what I got for wanting to go from API request to ggplot() in two steps.

@hadley
Copy link
Member

hadley commented Aug 27, 2022

Ok, if it's a JSON rectangling problem, the chances are it's now better solved with tidyr::unnest_wider() and tidyr::unnest_longer() and we don't need to worry about hammering the semantics of flatten out for this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants