You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pandas and the IngestionManagerPandas class for the FeatureStore class #2032
Describe the bug
To write from a Pandas dataframe to the Feature Store, IngestionManagerPandas iterates through the dataframe using the .iterrows() method. Pandas sends row values as floats; no matter what the datatype of the column. When the Feature Store tries to save the float to a column which was configured as an Integer, it throws an error. you can fix this by iterating through the dataframe with .loc or .iloc to get the proper datatype. This will likely confuse customers when they attempt to use the .ingest method in the SDK.
To reproduce
A clear, step-by-step set of instructions to reproduce the bug.
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots or logs
If applicable, add screenshots or logs to help explain your problem.
System information
A description of your system. Please provide:
SageMaker Python SDK version:
Framework name (eg. PyTorch) or algorithm (eg. KMeans):
Framework version:
Python version:
CPU or GPU:
Custom Docker image (Y/N):
Additional context
Add any other context about the problem here.
Which Python version do you see this bug on? We do have integ tests that writes int dtype columns to FeatureStore. I understand with iterrows we lose the dtype but the type of the data itself seems to have been mapped correctly in my experiment. Could you show me how to reproduce this error?
Your example is only valid because you have a string column in your dataframe, which makes the row to be defined as an object for the iterrow(). If there is no string column in the dataframe the iterrow() will understand it as a float series, which will cause the issue described by @aarsanjani .
Notes
Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames). For example,
To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster than iterrows.
@icywang86rui I edited your example so it works with itertuples, one point is that itertuples() doesnt return a Pandas Series so we need an extra treatment (_asdict() ) to iterate the items.
for _, row in data_frame[start_index:end_index].iterrows():
record = [
FeatureValue(feature_name=name, value_as_string=str(value))
for name, value in row.items()
]
could be changed to this:
for row in df.itertuples(index=False):
record = [
FeatureValue(feature_name=name, value_as_string=str(value))
for name, value in row._asdict().items()
]
I dont know exactly how to propose this with a pull/merge
@Previatto Thanks for the suggestion. This indeed was implemented with itertuples before. The problems is with itertuples when Python version is 3.6 or older and the number of column is larger than 256 regular tuple will be returned not named tuples. I see that you saw my PR that change from itertuples to iterrows.
I think i can switch back to itertuples and just iterate the tuple as regular tuple and get the column name from the dataframe itself instead.
Fixed. IngestionManagerPandas no longer uses df.iterrows() — which coerced integer columns to floats — and now iterates with df.itertuples(), which preserves per-column dtypes as suggested in this thread.
v3 (current sagemaker release): sagemaker/mlops/feature_store/ingestion_manager_pandas.py uses itertuples() across all ingestion paths.
Since the fix is shipped in both the maintained v2 line and v3, I'm closing this. Please reopen if you still hit a dtype mismatch on a current version.
Activity
icywang86rui commentedon Dec 15, 2020
@aarsanjani
Which Python version do you see this bug on? We do have integ tests that writes int dtype columns to FeatureStore. I understand with iterrows we lose the dtype but the type of the data itself seems to have been mapped correctly in my experiment. Could you show me how to reproduce this error?
altomi commentedon Dec 16, 2020
@icywang86rui
Your example is only valid because you have a string column in your dataframe, which makes the row to be defined as an object for the iterrow(). If there is no string column in the dataframe the iterrow() will understand it as a float series, which will cause the issue described by @aarsanjani .
Previatto commentedon Dec 16, 2020
From https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iterrows.html
Notes
Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames). For example,
To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster than iterrows.
@icywang86rui I edited your example so it works with itertuples, one point is that itertuples() doesnt return a Pandas Series so we need an extra treatment (_asdict() ) to iterate the items.
I believe that this line
sagemaker-python-sdk/src/sagemaker/feature_store/feature_group.py
Line 182 in e8deeb3
could be changed to this:
I dont know exactly how to propose this with a pull/merge
icywang86rui commentedon Dec 17, 2020
@Previatto Thanks for the suggestion. This indeed was implemented with itertuples before. The problems is with itertuples when Python version is 3.6 or older and the number of column is larger than 256 regular tuple will be returned not named tuples. I see that you saw my PR that change from itertuples to iterrows.
I think i can switch back to itertuples and just iterate the tuple as regular tuple and get the column name from the dataframe itself instead.
fix: use itertuples to ingest pandas dataframe to FeatureStore
fix: use itertuples to ingest pandas dataframe to FeatureStore (#2042)
fix: use itertuples to ingest pandas dataframe to FeatureStore (aws#2042
mohamedzeidan2021 commentedon Sep 1, 2026
Fixed.
IngestionManagerPandasno longer usesdf.iterrows()— which coerced integer columns to floats — and now iterates withdf.itertuples(), which preserves per-column dtypes as suggested in this thread.sagemakerrelease):sagemaker/mlops/feature_store/ingestion_manager_pandas.pyusesitertuples()across all ingestion paths.sagemaker/feature_store/feature_group.pyusesitertuples().Since the fix is shipped in both the maintained v2 line and v3, I'm closing this. Please reopen if you still hit a dtype mismatch on a current version.