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

add support for parsing newer pandas dtype #277

Merged
merged 3 commits into from
Jun 20, 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
18 changes: 9 additions & 9 deletions locopy/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ def find_column_type(dataframe, warehouse_type: str):

Following is the list of pandas data types that the function checks and their mapping in sql:

- bool -> boolean
- datetime64[ns] -> timestamp
- bool/pd.BooleanDtype -> boolean
- datetime64[ns, <tz>] -> timestamp
- M8[ns] -> timestamp
- int -> int
- float -> float
- int/pd.Int64Dtype -> int
- float/pd.Float64Dtype -> float
- float object -> float
- datetime object -> timestamp
- object -> varchar
- object/pd.StringDtype -> varchar

For all other data types, the column will be mapped to varchar type.

Expand Down Expand Up @@ -313,19 +313,19 @@ def validate_float_object(column):
data = dataframe[column].dropna().reset_index(drop=True)
if data.size == 0:
column_type.append("varchar")
elif data.dtype in ["datetime64[ns]", "M8[ns]"]:
elif (data.dtype in ["datetime64[ns]", "M8[ns]"]) or (re.match("(datetime64\[ns\,\W)([a-zA-Z]+)(\])",str(data.dtype))):
column_type.append("timestamp")
elif data.dtype == "bool":
elif str(data.dtype).lower().startswith("bool"):
column_type.append("boolean")
elif str(data.dtype).startswith("object"):
data_type = validate_float_object(data) or validate_date_object(data)
if not data_type:
column_type.append("varchar")
else:
column_type.append(data_type)
elif str(data.dtype).startswith("int"):
elif str(data.dtype).lower().startswith("int"):
column_type.append("int")
elif str(data.dtype).startswith("float"):
elif str(data.dtype).lower().startswith("float"):
column_type.append("float")
else:
column_type.append("varchar")
Expand Down
47 changes: 47 additions & 0 deletions tests/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from itertools import cycle
from pathlib import Path
from unittest import mock
import datetime

import pytest

Expand Down Expand Up @@ -340,6 +341,52 @@ def test_find_column_type():
assert find_column_type(input_text, "snowflake") == output_text_snowflake
assert find_column_type(input_text, "redshift") == output_text_redshift

def test_find_column_type_new():

from decimal import Decimal

import pandas as pd

input_text = pd.DataFrame.from_dict(
{
"a": [1],
"b": [pd.Timestamp('2017-01-01T12+0')],
"c": [1.2],
"d": ["a"],
"e": [True]
}
)

input_text = input_text.astype(
dtype={
"a": pd.Int64Dtype(),
"b": pd.DatetimeTZDtype(tz=datetime.timezone.utc),
"c": pd.Float64Dtype(),
"d": pd.StringDtype(),
"e": pd.BooleanDtype()
}
)

output_text_snowflake = {
"a": "int",
"b": "timestamp",
"c": "float",
"d": "varchar",
"e": "boolean",
}

output_text_redshift = {
"a": "int",
"b": "timestamp",
"c": "float",
"d": "varchar",
"e": "boolean",
}

assert find_column_type(input_text, "snowflake") == output_text_snowflake
assert find_column_type(input_text, "redshift") == output_text_redshift



def test_get_ignoreheader_number():
assert (
Expand Down
Loading