From 930639407803c6f9f21d798dfc98a30b68f4a7ec Mon Sep 17 00:00:00 2001 From: Mohamad Zamini <32536264+mzamini92@users.noreply.github.com> Date: Wed, 27 Sep 2023 18:36:03 -0600 Subject: [PATCH] improve the performance of base.py (#8610) This removes the use of the intermediate df list and directly concatenates the dataframes if path is a list of strings. The pd.concat function combines the dataframes efficiently, making it faster and more memory-efficient compared to appending dataframes to a list. --------- Co-authored-by: Harrison Chase --- .../langchain/agents/agent_toolkits/csv/base.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/langchain/langchain/agents/agent_toolkits/csv/base.py b/libs/langchain/langchain/agents/agent_toolkits/csv/base.py index f16b8772fd..90ab4f617d 100644 --- a/libs/langchain/langchain/agents/agent_toolkits/csv/base.py +++ b/libs/langchain/langchain/agents/agent_toolkits/csv/base.py @@ -24,11 +24,13 @@ def create_csv_agent( if isinstance(path, (str, IOBase)): df = pd.read_csv(path, **_kwargs) elif isinstance(path, list): - df = [] - for item in path: - if not isinstance(item, (str, IOBase)): - raise ValueError(f"Expected str or file-like object, got {type(path)}") - df.append(pd.read_csv(item, **_kwargs)) + if not all(isinstance(item, (str, IOBase)) for item in path): + raise ValueError( + f"Expected all elements in the list to be strings, got {type(path)}." + ) + dfs = [pd.read_csv(item, **_kwargs) for item in path] + df = pd.concat(dfs, ignore_index=True) else: - raise ValueError(f"Expected str, list, or file-like object, got {type(path)}") + raise ValueError(f"Expected str or list, got {type(path)}") + return create_pandas_dataframe_agent(llm, df, **kwargs)