Skip to content

Commit 2a268c4

Browse files
committed
add file_name param
1 parent 07b6075 commit 2a268c4

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

python-package/basedosdados/upload/table.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ def create_from_pandas(
509509
self,
510510
df: pd.DataFrame,
511511
partition_columns: Optional[list[str]] = None,
512+
file_name: str = "data",
512513
source_format: str = "csv",
513514
**kwargs,
514515
) -> None:
@@ -532,6 +533,7 @@ def create_from_pandas(
532533
partition_columns: A list of column names to partition the data by.
533534
These columns are removed from the data files and encoded in the
534535
storage path following the Hive partitioning scheme.
536+
file_name: Name of file without extension. Defaults to `data`.
535537
source_format: The format used to save the data. Only 'csv',
536538
'parquet' and 'avro' are supported. Defaults to 'csv'.
537539
**kwargs: Additional keyword arguments forwarded to `Table.create`
@@ -559,7 +561,7 @@ def create_from_pandas(
559561
missing_columns = [
560562
col for col in partition_columns if col not in df.columns
561563
]
562-
if missing_columns:
564+
if len(missing_columns) > 0:
563565
raise BaseDosDadosException(
564566
f"Partition columns not found in the DataFrame: {missing_columns}"
565567
)
@@ -572,16 +574,17 @@ def create_from_pandas(
572574
tmpdir=tmpdir,
573575
)
574576

575-
if partition_columns:
577+
if len(partition_columns) > 0:
576578
to_partitions(
577579
data=df,
578580
partition_columns=partition_columns,
579581
savepath=tmppath,
582+
file_name=file_name,
580583
file_format=source_format,
581584
)
582585
path = tmppath
583586
else:
584-
path = tmppath / f"data.{source_format}"
587+
path = tmppath / f"{file_name}.{source_format}"
585588
if source_format == "csv":
586589
df.to_csv(path, index=False)
587590
elif source_format == "parquet":

python-package/basedosdados/upload/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ def to_partitions(
1313
data: pd.DataFrame,
1414
partition_columns: List[str],
1515
savepath: str,
16+
file_name: str = "data",
1617
file_format: str = "csv",
1718
):
1819
"""Save data in to hive patitions schema, given a dataframe and a list of partition columns.
1920
Args:
2021
data (pandas.core.frame.DataFrame): Dataframe to be partitioned.
2122
partition_columns (list): List of columns to be used as partitions.
2223
savepath (str, pathlib.PosixPath): folder path to save the partitions
24+
file_name: Name of file without extension.
2325
file_format (str): The file format to save the partitioned data in.
2426
Only 'csv', 'parquet' and 'avro' are supported. Defaults to 'csv'.
2527
Exemple:
@@ -66,16 +68,17 @@ def to_partitions(
6668
filter_save_path.mkdir(parents=True, exist_ok=True)
6769

6870
if file_format == "csv":
69-
file_filter_save_path = Path(filter_save_path) / "data.csv"
70-
# append data to csv
71+
file_filter_save_path = (
72+
Path(filter_save_path) / f"{file_name}.csv"
73+
)
7174
df_filter.to_csv(
7275
file_filter_save_path,
7376
index=False,
74-
mode="a",
75-
header=not file_filter_save_path.exists(),
7677
)
7778
elif file_format == "parquet":
78-
file_filter_save_path = Path(filter_save_path) / "data.parquet"
79+
file_filter_save_path = (
80+
Path(filter_save_path) / f"{file_name}.parquet"
81+
)
7982
df_filter.to_parquet(file_filter_save_path, index=False)
8083
elif file_format == "avro":
8184
try:
@@ -85,7 +88,9 @@ def to_partitions(
8588
"Optional dependencies for handling AVRO files are not installed. "
8689
'Please install basedosdados with the "avro" extra'
8790
) from exc
88-
file_filter_save_path = Path(filter_save_path) / "data.avro"
91+
file_filter_save_path = (
92+
Path(filter_save_path) / f"{file_name}.avro"
93+
)
8994
pandavro.to_avro(str(file_filter_save_path), df_filter)
9095
else:
9196
raise NotImplementedError(

0 commit comments

Comments
 (0)