Skip to content

Commit

Permalink
Adding solution to challenge in episode 4
Browse files Browse the repository at this point in the history
  • Loading branch information
wood-chris committed Feb 28, 2024
1 parent 0b10887 commit 584790c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions _episodes/04-data-types-and-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,41 @@ dtype('float64')
>
> Next try converting `Temperature` to an integer. What goes wrong here? What is Pandas telling you?
> We will talk about some solutions to this in the section below.
>> ## Solution
>>
>> Converting the `buoy` column to floats returns
>> ~~~
>> 0 14.0
>> 1 7.0
>> 2 5.0
>> 3 3.0
>> 4 10.0
>> ...
>> 2068 16.0
>> 2069 16.0
>> 2070 16.0
>> 2071 16.0
>> 2072 16.0
>> Name: buoy_id, Length: 2073, dtype: float64
>> ~~~
>> {: .output}
>> So we can see that we can convert a whole column of _int_ values to floating point values. Converting floating point values to ints works in the same way.
>> However, this only works _if_ all there's a value in
>> every row. When we try this with the Temperature column:
>> ~~~
>> waves_df.Temperature.astype("int")
>> ~~~
>> {: .language-python}
>>
>> We get an error, with the pertinent line in the error (the last one) being:
>>
>> ~~~
>> ValueError: Cannot convert NA to integer
>> ~~~
>> {: .output}
>>
>> This happens because some of the values in the Temperature column are None values, and the `astype` function can't convert this type of value.
> {: .solution}
{: .challenge}
## Missing Data Values - NaN
Expand Down

0 comments on commit 584790c

Please sign in to comment.