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

Added excel normalization code and associated tests #3132

Merged
merged 7 commits into from
Aug 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion mathesar/imports/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,28 @@ def insert_records_from_dataframe(name, schema, column_names, engine, comment, d
return table


def remove_empty_rows_and_columns_from_dataframe(df):
if df.iloc[0].isna().any():

# drop rows with all NaN values
df.dropna(how='all', inplace=True)

# drop columns with all NaN values
df.dropna(axis=1, how='all', inplace=True)

if all(df.columns.str.startswith('Unnamed')):
df.columns = df.iloc[0]
df = df[1:]
Comment on lines +40 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a conderation here for whether or not the first row of the data is a header in #3030.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is planned for the coming PRs.


return df


def create_db_table_from_excel_data_file(data_file, name, schema, comment=None):
db_name = schema.database.name
engine = create_mathesar_engine(db_name)
dataframe = pandas.read_excel(data_file.file.path)
dataframe = remove_empty_rows_and_columns_from_dataframe(
pandas.read_excel(data_file.file.path)
)
column_names = process_column_names(dataframe.columns)
try:
table = insert_records_from_dataframe(name, schema, column_names, engine, comment, dataframe)
Expand Down
25 changes: 25 additions & 0 deletions mathesar/tests/api/test_table_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ def missing_keys_json_data_file():
return data_file


@pytest.fixture
def misaligned_table_excel_data_file():
data_filepath = 'mathesar/tests/data/excel_parsing/misaligned_table.xlsx'
with open(data_filepath, "rb") as excel_file:
data_file = DataFile.objects.create(
file=File(excel_file),
created_from='file',
base_name='missaligned_table',
type='excel'
)
return data_file


@pytest.fixture
def schema_name():
return 'table_tests'
Expand Down Expand Up @@ -1881,3 +1894,15 @@ def test_create_table_with_nested_json_objects(client, schema):
client, table_name, table_name, datafile, schema, expected_data[index]["first_row"],
expected_data[index]["column_names"], import_target_table=None
)


def test_create_table_and_normalize_excel_data_file(client, misaligned_table_excel_data_file, schema):
table_name = 'misaligned_table'
expt_name = get_expected_name(table_name, data_file=misaligned_table_excel_data_file)
first_row = (1, 'John', '25', 'Male')
column_names = ["Name", "Age", "Gender"]

check_create_table_response(
client, table_name, expt_name, misaligned_table_excel_data_file, schema, first_row,
column_names, import_target_table=None
)
Binary file not shown.