1
$\begingroup$

I have used the following code before and it worked then but now it seems to default to the word Function, and some of the other commands are giving results like All when they used to work.

Would you help clarify what's going on?

data = ResourceData["Epidemic Data for Novel Coronavirus COVID-19", 
   "USStates"][
  All, {#AdministrativeDivision, #ConfirmedCases["LastValue"]} &]
usaStateCases[s_] := 
       Select[data, MatchQ[Interpreter["USState"][s], First[#]] &][
          All, #ConfirmedCases["LastValue"] &] // Normal // First
        
usaStateDeaths[s_] := 
       Select[data, MatchQ[Interpreter["USState"][s], First[#]] &][
          All, #Deaths["LastValue"] &] // Normal // First
        
usaStateCases["California"] 

This used to work but now, the output of the last line is Function.

$\endgroup$
  • 4
    $\begingroup$ We cannot test your code because you did not provide the contents of data. Can you give a source or an example? $\endgroup$ – MarcoB Jun 27 at 16:37
  • $\begingroup$ Thank you. Please note my edit. TIA. $\endgroup$ – Veritas Lux Jun 28 at 2:37
3
$\begingroup$

It is unlikely that the OP $data$ ever worked for 2 reasons. First, in the definition $data$, the "LastValue" property of a TimeSeries object is called returning an Integer in the second column. Integer has no "LastValue" property that is subsequently called in the $usaStateXXX$ functions.

data[[1, 2]] // Normal // Head
(* Integer *)

Second, the definition of $data$ is not transforming columns, rather it is returning a new list of lists of functions applied to columns. This can be seen in the documentation for Dataset under the details section shown here.

Dataset Details

Inspection of $data$, shows that it is a list of lists with no column names.

data

data Dataset displayed

If it is desired for the $usaStateXXX$ functions to work, just build $data$ by providing the column keys as shown:

data = ResourceData["Epidemic Data for Novel Coronavirus COVID-19", 
   "USStates"][All, {"AdministrativeDivision", "ConfirmedCases"}]

Extended Response to flinty's Comment

I am a Dataset novice versus a Dataset jock. My main motivation to answering MSE questions is to build MMA skills in areas that I am deficient, so any help is welcome.

In this case, let's look at a simple dataset from the documentation.

dataset = Dataset[{
   <|"a" -> 1, "b" -> "x", "c" -> {1}|>,
   <|"a" -> 2, "b" -> "y", "c" -> {2, 3}|>,
   <|"a" -> 3, "b" -> "z", "c" -> {3}|>,
   <|"a" -> 4, "b" -> "x", "c" -> {4, 5}|>,
   <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
   <|"a" -> 6, "b" -> "z", "c" -> {}|>}]

Now, let's examine three ways to operate on the dataset and see what it returns.

1. Take Specific Columns of the Dataset Using Keys

If we want to take specific columns of the dataset using keys, then it will return a list of associations as can be seen by the dataset formatting or the Normal representation.

dataset[All, {"a", "b"}]
dataset[All, {"a", "b"}] // Normal

Keys image

2. Apply a List of Functions to All Rows (OP Case)

If we apply a list of anonymous functions to the dataset, then it will return a list of lists. As such, it lost its column headings so that it cannot be used in subsequent operations. This is similar to how $data$ was constructed in the OP.

dataset[All, {#a, #b} &]
dataset[All, {#a, #b} &] // Normal

Applying list of functions

I would say that this is "unlikely" to be a new behavior in dataset, but I am novice.

3. Apply Functions to Columns Returning Dataset with Column Names

To apply functions to columns and retain or create new column headings, we need to use associations as shown in the Dataset Documentation.

dataset[All, <|"a'" -> #a, "b'" -> #b|> &]
dataset[All, <|"a'" -> #a, "b'" -> #b|> &] // Normal

Functions with Column Names in Dataset

Conclusion

Independent of whether there has been a change in the ResourceFunction, operating on the data like case 2 will return a dataset without column headers (at least for my version 12.1.1 for Microsoft Windows (64-bit) (June 19, 2020)). The subsequent functions rely on those column headers. Therefore, I considered it "unlikely" that it worked. Someone would have to look at another version to see if the operation returned a list of associations, but then one would have to contend with the Integer type not having a "LastValue" property as in the TimeSeries object.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Tim, I can only see a difference in the data statement. If that’s right, then perhaps you should remove the rest of the code that is identical. It would also be helpful if you could explain your change. $\endgroup$ – MarcoB Jun 28 at 3:51
  • $\begingroup$ @MarcoB I tried to incorporate your comments. Sometimes I get requests to include the entire workflow, but you are probably correct. I guess I was in a rush to sneak an answer before bedtime. $\endgroup$ – Tim Laska Jun 30 at 2:44
  • $\begingroup$ thank you for the edits. It makes a lot more sense to me now. (+1) $\endgroup$ – MarcoB Jun 30 at 13:36
  • $\begingroup$ It is unlikely that the OP data ever worked for 2 reasons No it really did work at one point because I provided this code and got good numbers - I think what's happened is the data has changed layout after a resource update from wolfram, but you'll just have to take my word on that because I can't get back to the original data and now I'm seeing the errors OP gets. $\endgroup$ – flinty Jun 30 at 13:48
  • $\begingroup$ @flinty I added an extended response to your comment at the end of the answer. Using a simple test dataset from the Basic Examples section of Dataset shows that the OP construction should return a list of lists versus a list of associations (needed to refer to column headers) that would be required for the subsequent functions to work. So, it does not require the ResourceFunction to change only that the behavior of Dataset has changed. That is why I considered it "unlikely". $\endgroup$ – Tim Laska Jul 1 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.