Skip to content

Commit

Permalink
fix: replace empty '' by numpy.nan on column for google-sheet [TCTC-6…
Browse files Browse the repository at this point in the history
…530] (#1228)
  • Loading branch information
Sanix-Darker authored Aug 22, 2023
1 parent 06205f0 commit 8fa567c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
35 changes: 35 additions & 0 deletions tests/google_sheets/test_google_sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime
from os import path

import numpy as np
import pandas as pd
from googleapiclient.http import HttpMock
from pandas import DataFrame
Expand All @@ -15,6 +16,8 @@
from toucan_connectors.google_sheets.google_sheets_connector import (
GoogleSheetsConnector,
GoogleSheetsDataSource,
parse_cell_value,
serial_number_to_date,
)
from toucan_connectors.json_wrapper import JsonWrapper

Expand Down Expand Up @@ -298,3 +301,35 @@ def test_get_form(mocker):
)
expected_results = ['sample data', 'animals']
assert schema['definitions']['sheet']['enum'] == expected_results


def test_numeric_dateformat_():
value = 44303 # Example serial number representing a date
format_ = {'numberFormat': {'type': 'DATE'}}
result = parse_cell_value(value, format_)
assert result == serial_number_to_date(value)


def test_empty_string():
value = ''
format_ = {}
result = parse_cell_value(value, format_)
assert np.isnan(result)


def test_other_types():
value = 'hello'
format_ = {'numberFormat': {'type': 'DATE'}}
result = parse_cell_value(value, format_)
assert result == value

value = 123
format_ = {}
result = parse_cell_value(value, format_)
assert result == value


def test_default_format():
value = 44303 # Example serial number representing a date
result = parse_cell_value(value)
assert result == value
13 changes: 8 additions & 5 deletions toucan_connectors/google_sheets/google_sheets_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
from typing import Any, Callable, Dict, List, Optional, Type

import numpy as np
import pandas as pd
from dateutil.relativedelta import relativedelta
from google.oauth2.credentials import Credentials
Expand Down Expand Up @@ -214,17 +215,19 @@ def serial_number_to_date(serial_number: float) -> datetime:
return SERIAL_REFERENCE_DAY + relativedelta(days=int(serial_number))


def parse_cell_value(value, format):
def parse_cell_value(value: Any, format_: dict[str, Any] | None = None) -> Any:
"""
Use the format (if provided) to parse the value in its intended type
"""
if (
(type(value) is int or type(value) is float)
and format
and 'numberFormat' in format
and format['numberFormat']['type'] == 'DATE'
isinstance(value, (int, float))
and format_ is not None
and format_.get('numberFormat', {}).get('type') == 'DATE'
):
return serial_number_to_date(value)
elif isinstance(value, str) and value == '':
return np.nan

return value


Expand Down

0 comments on commit 8fa567c

Please sign in to comment.