Skip to content

[ENH] Updated convert_excel_date to throw meaningful error #841

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

Merged
merged 4 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
to provide a simpler selection without the need for lists. - @samukweku
- [ENH] `encode_categoricals` refactored to use generic functions
via `functools.dispatch`. - @samukweku
- [ENH] Updated convert_excel_date to throw meaningful error when values contain non-numeric. @nvamsikrishna05

## [v0.20.14] - 2021-03-25

Expand Down
Binary file modified examples/notebooks/dirty_data.xlsx
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/notebooks/inflating_converting_currency.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
" lambda x: x.inflate_currency(\n",
" column_name='expenditure',\n",
" country='USA',\n",
" currency_year=x.name,\n",
" currency_year=int(x.name),\n",
" to_year=2018,\n",
" make_new_column=True\n",
" )\n",
Expand Down Expand Up @@ -271,4 +271,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
9 changes: 9 additions & 0 deletions janitor/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype
import pandas_flavor as pf
from multipledispatch import dispatch
from natsort import index_natsorted
Expand Down Expand Up @@ -1089,7 +1090,15 @@ def convert_excel_date(
:param df: A pandas DataFrame.
:param column_name: A column name.
:returns: A pandas DataFrame with corrected dates.
:raises ValueError: if There are non numeric values in the column.
""" # noqa: E501

if not is_numeric_dtype(df[column_name]):
raise ValueError(
"There are non-numeric values in the column. \
All values must be numeric"
)

df[column_name] = pd.TimedeltaIndex(
df[column_name], unit="d"
) + dt.datetime(
Expand Down
12 changes: 12 additions & 0 deletions tests/functions/test_convert_excel_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ def test_convert_excel_date():
)

assert df["hire_date"].dtype == "M8[ns]"


@pytest.mark.functions
def test_convert_excel_date_with_string_data():
""" Raises ValueError if values of column are not numeric """
df = pd.read_excel(
Path(pytest.EXAMPLES_DIR) / "notebooks" / "dirty_data.xlsx",
engine="openpyxl",
).clean_names()

with pytest.raises(ValueError):
df.convert_excel_date("hire_date_str")