aws-samples / aws-samples/aws-cloudwatch-dashboard-generator

Bug: Python will have runtime error "KeyError: nan" if there has empty line at input .csv file

Open
#7 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
25
Forks
6
PR merge metrics
No merged PRs in 30d

Description

If I unexpectedly added a blank line at the last line at Input .csv file such like
```
service,region_code,dim1_name,dim1_value,dim2_name,dim2_value
ALB,ap-northeast-1,LoadBalancer,app/testALB/12345,
ALB,ap-northeast-1,LoadBalancer,app/testALB2/1233333,
EC2,ap-northeast-1,InstanceId,12333,,
EC2,ap-northeast-1,InstanceId,i-avb,,
,,,,,
```
Then I will get below error when run ` iemcwd.py`

```
$ python3 iemcwd.py -f inputs/csv/emptyline.csv -o outputs/emptyline.json
Traceback (most recent call last):
File "/Users/wumark/workspace/src/aws-cloudwatch-dashboard-generator/aws-cloudwatch-dashboard-generator/iemcwd.py", line 44, in
main()
File "/Users/wumark/workspace/src/python3_11/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/python3_11/lib/python3.11/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/python3_11/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/python3_11/lib/python3.11/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/aws-cloudwatch-dashboard-generator/aws-cloudwatch-dashboard-generator/iemcwd.py", line 27, in main
input_dict = utils.read_csv(input_file)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/aws-cloudwatch-dashboard-generator/aws-cloudwatch-dashboard-generator/utils.py", line 36, in read_csv
input_file = _csv_resources_to_dict(df)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/aws-cloudwatch-dashboard-generator/aws-cloudwatch-dashboard-generator/utils.py", line 64, in _csv_resources_to_dict
df.groupby("service").get_group(service)["region_code"].unique()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/wumark/workspace/src/python3_11/lib/python3.11/site-packages/pandas/core/groupby/groupby.py", line 800, in get_group
raise KeyError(name)
KeyError: nan
```

I expect iemcwd.py should handle this expect and ignored empty line

Contributor guide

Open the contributing guide

Research direction

Start with iemcwd.py and follow its call to utils.py, especially _csv_resources_to_dict, using the command shown in the report with inputs/csv/emptyline.csv. Confirm how the CSV is loaded and grouped, then verify that a trailing blank row is ignored and the command completes without KeyError: nan.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, pandas, python
Domain
cli, data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.