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

Sharepoint convert data types to string #937

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added `_empty_column_to_string` and `_convert_all_to_string_type` to convert data types to string.
- Added `na_values` parameter to `Sharepoint` class to parse `N/A` values coming from the excel file columns.
- Added `get_last_segment_from_url` function to sharepoint file.
- Added `validate` function to `viadot/utils.py`
Expand Down
38 changes: 37 additions & 1 deletion src/viadot/sources/sharepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ def _download_excel(self, url: str, **kwargs) -> pd.ExcelFile:
bytes_stream = io.BytesIO(response.content)
return pd.ExcelFile(bytes_stream)

def _convert_all_to_string_type(self, df: pd.DataFrame) -> pd.DataFrame:
"""Convert all column data types in the DataFrame to strings.

This method converts all the values in the DataFrame to strings,
handling NaN values by replacing them with None.

Args:
df (pd.DataFrame): DataFrame to convert.

Returns:
pd.DataFrame: DataFrame with all data types converted to string.
Columns that contain only None values are also
converted to string type.
"""
df_converted = df.astype(str).where(pd.notnull(df), None)
return self._empty_column_to_string(df=df_converted)

def _empty_column_to_string(self, df: pd.DataFrame) -> pd.DataFrame:
"""Convert the type of columns containing only None values to string.

This method iterates through the DataFrame columns and converts the
type of any column that contains only None values to string.

Args:
df (pd.DataFrame): DataFrame to convert.

Returns:
pd.DataFrame: Updated DataFrame with columns containing only
None values converted to string type. All columns
in the returned DataFrame will be of type object/string.
"""
for col in df.columns:
if df[col].isnull().all():
df[col] = df[col].astype("string")
return df

@add_viadot_metadata_columns
def to_df(
self,
Expand Down Expand Up @@ -218,4 +254,4 @@ def to_df(
if tests:
validate(df=df_clean, tests=tests)

return df_clean.astype(str).where(pd.notnull(df_clean), None)
return self._convert_all_to_string_type(df=df_clean)
36 changes: 30 additions & 6 deletions tests/unit/test_sharepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,53 @@
import pandas as pd
from viadot.sources import Sharepoint

DUMMY_CREDS = {"site": "test", "username": "test2", "password": "test"}
SAMPLE_DF = pd.DataFrame(
{
"int_col": [1, 2, 3, 4, 5, None],
"float_col": [1.1, 2.2, 3.3, 3.0, 5.5, 6.6],
"str_col": ["a", "b", "c", "d", "e", "f"],
"nan_col": [None, None, None, None, None, None],
"mixed_col": [1, "text", None, None, 4.2, "text2"],
}
)


class SharepointMock(Sharepoint):
def _download_excel(self, url=None):
return pd.ExcelFile(Path("tests/unit/test_file.xlsx"))


def test_sharepoint_default_na():
dummy_creds = {"site": "test", "username": "test2", "password": "test"}

s = SharepointMock(credentials=dummy_creds)
s = SharepointMock(credentials=DUMMY_CREDS)
df = s.to_df(url="test", na_values=Sharepoint.DEFAULT_NA_VALUES)

assert not df.empty
assert "NA" not in list(df["col_a"])


def test_sharepoint_custom_na():
dummy_creds = {"site": "test", "username": "test", "password": "test"}

s = SharepointMock(credentials=dummy_creds)
s = SharepointMock(credentials=DUMMY_CREDS)
df = s.to_df(
url="test", na_values=[v for v in Sharepoint.DEFAULT_NA_VALUES if v != "NA"]
)

assert not df.empty
assert "NA" in list(df["col_a"])


def test_sharepoint_convert_all_to_string_type():
s = SharepointMock(credentials=DUMMY_CREDS)
converted_df = s._convert_all_to_string_type(df=SAMPLE_DF)

assert not converted_df.empty
assert pd.isnull(converted_df["nan_col"]).all()


def test_sharepoint_convert_empty_columns_to_string():
s = SharepointMock(credentials=DUMMY_CREDS)
converted_df = s._empty_column_to_string(df=SAMPLE_DF)

assert not converted_df.empty
assert converted_df["float_col"].dtype == float
assert converted_df["nan_col"].dtype == "string"
Loading