Update README.md
Browse files
README.md
CHANGED
|
@@ -79,13 +79,13 @@ import pandas as pd
|
|
| 79 |
from datetime import datetime
|
| 80 |
import gc
|
| 81 |
|
| 82 |
-
def load_csvs_from_huggingface(start_date, end_date):
|
| 83 |
"""
|
| 84 |
Load only the necessary CSV files from a Hugging Face dataset repository.
|
| 85 |
|
| 86 |
:param start_date: str, the start date in 'YYYY-MM-DD' format (inclusive)
|
| 87 |
:param end_date: str, the end date in 'YYYY-MM-DD' format (inclusive)
|
| 88 |
-
|
| 89 |
:return: pd.DataFrame, combined data from selected CSVs
|
| 90 |
"""
|
| 91 |
|
|
@@ -140,6 +140,13 @@ def load_csvs_from_huggingface(start_date, end_date):
|
|
| 140 |
on_bad_lines="skip"
|
| 141 |
)
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
# 2. Tell HuggingFace to output Pandas dataframes when sliced
|
| 144 |
dataset = dataset.with_format("pandas")
|
| 145 |
|
|
@@ -175,8 +182,18 @@ def load_csvs_from_huggingface(start_date, end_date):
|
|
| 175 |
except Exception as e:
|
| 176 |
print(f"Error processing {filepath}: {e}")
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
|
| 182 |
```
|
|
|
|
| 79 |
from datetime import datetime
|
| 80 |
import gc
|
| 81 |
|
| 82 |
+
def load_csvs_from_huggingface(start_date, end_date, columns_to_keep=None):
|
| 83 |
"""
|
| 84 |
Load only the necessary CSV files from a Hugging Face dataset repository.
|
| 85 |
|
| 86 |
:param start_date: str, the start date in 'YYYY-MM-DD' format (inclusive)
|
| 87 |
:param end_date: str, the end date in 'YYYY-MM-DD' format (inclusive)
|
| 88 |
+
:param columns_to_keep: list of str, optional. Specific columns to load (e.g., ["date", "lang"]).
|
| 89 |
:return: pd.DataFrame, combined data from selected CSVs
|
| 90 |
"""
|
| 91 |
|
|
|
|
| 140 |
on_bad_lines="skip"
|
| 141 |
)
|
| 142 |
|
| 143 |
+
# Select Columns Before Chunking
|
| 144 |
+
if columns_to_keep is not None:
|
| 145 |
+
# Safety check: Only select columns that actually exist in this specific file
|
| 146 |
+
valid_columns = [col for col in columns_to_keep if col in dataset.column_names]
|
| 147 |
+
if valid_columns:
|
| 148 |
+
dataset = dataset.select_columns(valid_columns)
|
| 149 |
+
|
| 150 |
# 2. Tell HuggingFace to output Pandas dataframes when sliced
|
| 151 |
dataset = dataset.with_format("pandas")
|
| 152 |
|
|
|
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Error processing {filepath}: {e}")
|
| 184 |
|
| 185 |
+
|
| 186 |
+
if not df_list:
|
| 187 |
+
return pd.DataFrame()
|
| 188 |
+
|
| 189 |
+
# Combine chunks
|
| 190 |
+
final_df = pd.concat(df_list, ignore_index=True)
|
| 191 |
+
|
| 192 |
+
# Destroy the list
|
| 193 |
+
del df_list
|
| 194 |
+
gc.collect()
|
| 195 |
+
|
| 196 |
+
return final_df
|
| 197 |
|
| 198 |
|
| 199 |
```
|