Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should we forbid converting str to numeric values in Column.set()? #311

Open
hagenw opened this issue Sep 16, 2022 · 3 comments
Open

Should we forbid converting str to numeric values in Column.set()? #311

hagenw opened this issue Sep 16, 2022 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@hagenw
Copy link
Member

hagenw commented Sep 16, 2022

When using audformat.Column.set() we only check if the provided value(s) match a corresponding scheme if the scheme has labels or a minimum and or maximum.

Let's start with a database containing an integer scheme:

import audformat

db = audformat.Database('db')
db.schemes['scheme'] = audformat.Scheme('int')
db['table'] = audformat.Table(audformat.filewise_index('f1'))
db['table']['column'] = audformat.Column(scheme_id='scheme')

If we assign a string value it is converted to integer automatically:

>>> db['table']['column'].set('1')
>>> db['table']['column'].get()
file
f1    1
Name: column, dtype: Int64

But if a user provides a string that cannot be converted to the requested numeric type she will get an error message that might be not that helpful:

>>> db['table']['column'].set('a1')
...
ValueError: invalid literal for int() with base 10: 'a1'

There are two possible options to enhance this:

  • forbid assigning strings to numeric schemes
  • catch the ValueError and raise another scheme specific ValueError stating that the provided value cannot be converted to the requested dtype

/cc @hesamsagha

@hagenw hagenw added enhancement New feature or request question Further information is requested labels Sep 16, 2022
@frankenjoe
Copy link
Collaborator

catch the ValueError and raise another scheme specific ValueError stating that the provided value cannot be converted to the requested dtype

Seems like the easier solution since there might be other implicit conversions we are not aware of.

@hagenw
Copy link
Member Author

hagenw commented Jan 18, 2023

Yes, I would also be in favor of raising an error with a better error message.

@hagenw
Copy link
Member Author

hagenw commented Apr 13, 2023

On the other hand catching the error might also hide the underlying problem, e.g. #364 would have been very hard to understand by a user if we would have catched the ValueError and replaced it with a custom message stating that the user cannot provide dates to a column with a date scheme ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants