Skip to content

Commit

Permalink
Merge pull request #1445 from Avaiga/fix/#1199-support-pandas-series-…
Browse files Browse the repository at this point in the history
…signature-in-_convert_data_to_dataframe-method

Fix/#1199 - Support pandas series signature in  _convert_data_to_dataframe()
  • Loading branch information
trgiangdo authored Jun 26, 2024
2 parents 0e9e9bf + d3e83f5 commit d68654c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions taipy/core/data/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _write(self, data: Any):
)
else:
self._convert_data_to_dataframe(exposed_type, data).to_csv(
self._path, index=False, encoding=self.properties[self.__ENCODING_KEY], header=None
self._path, index=False, encoding=self.properties[self.__ENCODING_KEY], header=False
)

def write_with_column_names(self, data: Any, columns: Optional[List[str]] = None, job_id: Optional[JobId] = None):
Expand All @@ -201,6 +201,6 @@ def write_with_column_names(self, data: Any, columns: Optional[List[str]] = None
"""
df = self._convert_data_to_dataframe(self.properties[self._EXPOSED_TYPE_PROPERTY], data)
if columns and isinstance(df, pd.DataFrame):
df.columns = columns
df.columns = pd.Index(columns, dtype="object")
df.to_csv(self._path, index=False, encoding=self.properties[self.__ENCODING_KEY])
self.track_edit(timestamp=datetime.now(), job_id=job_id)
2 changes: 1 addition & 1 deletion taipy/core/data/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def __write_excel_with_multiple_sheets(self, data: Any, columns: List[str] = Non
if columns:
data[key].columns = columns

df.to_excel(writer, key, index=False, header=self.properties[self._HAS_HEADER_PROPERTY] or None)
df.to_excel(writer, key, index=False, header=self.properties[self._HAS_HEADER_PROPERTY] or False)

def _write(self, data: Any):
if isinstance(data, Dict):
Expand Down
8 changes: 4 additions & 4 deletions taipy/core/data/parquet.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ def write_with_kwargs(self, data: Any, job_id: Optional[JobId] = None, **write_k
}
kwargs.update(self.properties[self.__WRITE_KWARGS_PROPERTY])
kwargs.update(write_kwargs)
if isinstance(data, pd.Series):
df = pd.DataFrame(data)
else:
df = self._convert_data_to_dataframe(self.properties[self._EXPOSED_TYPE_PROPERTY], data)

df = self._convert_data_to_dataframe(self.properties[self._EXPOSED_TYPE_PROPERTY], data)
if isinstance(df, pd.Series):
df = pd.DataFrame(df)

# Ensure that the columns are strings, otherwise writing will fail with pandas 1.3.5
df.columns = df.columns.astype(str)
Expand Down
12 changes: 9 additions & 3 deletions taipy/core/data/sql_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# specific language governing permissions and limitations under the License.

from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Set
from typing import Any, Dict, List, Optional, Set, Union

import pandas as pd
from sqlalchemy import MetaData, Table
Expand Down Expand Up @@ -146,8 +146,14 @@ def __insert_dicts(cls, data: List[Dict], table: Any, connection: Any, delete_ta
connection.execute(table.insert(), data)

@classmethod
def __insert_dataframe(cls, df: pd.DataFrame, table: Any, connection: Any, delete_table: bool) -> None:
cls.__insert_dicts(df.to_dict(orient="records"), table, connection, delete_table)
def __insert_dataframe(
cls, df: Union[pd.DataFrame, pd.Series], table: Any, connection: Any, delete_table: bool
) -> None:
if isinstance(df, pd.Series):
data = [df.to_dict()]
elif isinstance(df, pd.DataFrame):
data = df.to_dict(orient="records")
cls.__insert_dicts(data, table, connection, delete_table)

@classmethod
def __delete_all_rows(cls, table: Any, connection: Any, delete_table: bool) -> None:
Expand Down

0 comments on commit d68654c

Please sign in to comment.