forked from sinaptik-ai/pandas-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_importer.py
32 lines (25 loc) · 937 Bytes
/
file_importer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pandasai.pandas as pd
from .from_google_sheets import from_google_sheets
class FileImporter:
"""
Class to import a dataframe from a file (csv, parquet, xlsx)
"""
@staticmethod
def import_from_file(file_path: str) -> pd.DataFrame:
"""
Import a dataframe from a file (csv, parquet, xlsx)
Args:
file_path (str): Path to the file to be imported.
Returns:
pd.DataFrame: Pandas dataframe
"""
if file_path.endswith(".csv"):
return pd.read_csv(file_path)
elif file_path.endswith(".parquet"):
return pd.read_parquet(file_path)
elif file_path.endswith(".xlsx"):
return pd.read_excel(file_path)
elif file_path.startswith("https://docs.google.com/spreadsheets/"):
return from_google_sheets(file_path)[0]
else:
raise ValueError("Invalid file format.")