Skip to content

Commit 9e8387c

Browse files
authored
ENH: Move warnings to error/__init__.py per GH27656 (#47901)
* ENH: Move warnings to error/__init__.py per GH27656 * ENH: update whatsnew line * ENH: add and re-add final * ENH: add to __all__ * ENH: apply feedback * ENH: fix doc string * ENH: add additional exception/warnings to rst * ENH: fix rst and typo
1 parent 3670fbf commit 9e8387c

File tree

5 files changed

+76
-18
lines changed

5 files changed

+76
-18
lines changed

doc/source/reference/testing.rst

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Exceptions and warnings
2727
errors.AbstractMethodError
2828
errors.AccessorRegistrationWarning
2929
errors.AttributeConflictWarning
30+
errors.CategoricalConversionWarning
3031
errors.ClosedFileError
3132
errors.CSSWarning
3233
errors.DatabaseError
@@ -36,6 +37,7 @@ Exceptions and warnings
3637
errors.EmptyDataError
3738
errors.IncompatibilityWarning
3839
errors.IndexingError
40+
errors.InvalidColumnName
3941
errors.InvalidIndexError
4042
errors.IntCastingNaNError
4143
errors.MergeError
@@ -49,6 +51,7 @@ Exceptions and warnings
4951
errors.ParserWarning
5052
errors.PerformanceWarning
5153
errors.PossibleDataLossError
54+
errors.PossiblePrecisionLoss
5255
errors.PyperclipException
5356
errors.PyperclipWindowsException
5457
errors.SettingWithCopyError
@@ -57,6 +60,7 @@ Exceptions and warnings
5760
errors.UndefinedVariableError
5861
errors.UnsortedIndexError
5962
errors.UnsupportedFunctionCall
63+
errors.ValueLabelTypeMismatch
6064

6165
Bug report function
6266
-------------------

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ Other enhancements
275275
- A :class:`errors.PerformanceWarning` is now thrown when using ``string[pyarrow]`` dtype with methods that don't dispatch to ``pyarrow.compute`` methods (:issue:`42613`)
276276
- Added ``numeric_only`` argument to :meth:`Resampler.sum`, :meth:`Resampler.prod`, :meth:`Resampler.min`, :meth:`Resampler.max`, :meth:`Resampler.first`, and :meth:`Resampler.last` (:issue:`46442`)
277277
- ``times`` argument in :class:`.ExponentialMovingWindow` now accepts ``np.timedelta64`` (:issue:`47003`)
278-
- :class:`.DataError`, :class:`.SpecificationError`, :class:`.SettingWithCopyError`, :class:`.SettingWithCopyWarning`, :class:`.NumExprClobberingError`, :class:`.UndefinedVariableError`, and :class:`.IndexingError` are now exposed in ``pandas.errors`` (:issue:`27656`)
278+
- :class:`.DataError`, :class:`.SpecificationError`, :class:`.SettingWithCopyError`, :class:`.SettingWithCopyWarning`, :class:`.NumExprClobberingError`, :class:`.UndefinedVariableError`, :class:`.IndexingError`, :class:`.PyperclipException`, :class:`.PyperclipWindowsException`, :class:`.CSSWarning`, :class:`.PossibleDataLossError`, :class:`.ClosedFileError`, :class:`.IncompatibilityWarning`, :class:`.AttributeConflictWarning`, :class:`.DatabaseError, :class:`.PossiblePrecisionLoss, :class:`.ValueLabelTypeMismatch, :class:`.InvalidColumnName, and :class:`.CategoricalConversionWarning` are now exposed in ``pandas.errors`` (:issue:`27656`)
279279
- Added ``check_like`` argument to :func:`testing.assert_series_equal` (:issue:`47247`)
280280
- Allow reading compressed SAS files with :func:`read_sas` (e.g., ``.sas7bdat.gz`` files)
281281
- :meth:`DatetimeIndex.astype` now supports casting timezone-naive indexes to ``datetime64[s]``, ``datetime64[ms]``, and ``datetime64[us]``, and timezone-aware indexes to the corresponding ``datetime64[unit, tzname]`` dtypes (:issue:`47579`)

pandas/errors/__init__.py

+60
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,67 @@ class DatabaseError(OSError):
479479
"""
480480

481481

482+
class PossiblePrecisionLoss(Warning):
483+
"""
484+
Warning raised by to_stata on a column with a value outside or equal to int64.
485+
486+
When the column value is outside or equal to the int64 value the column is
487+
converted to a float64 dtype.
488+
489+
Examples
490+
--------
491+
>>> df = pd.DataFrame({"s": pd.Series([1, 2**53], dtype=np.int64)})
492+
>>> df.to_stata('test') # doctest: +SKIP
493+
... # PossiblePrecisionLoss: Column converted from int64 to float64...
494+
"""
495+
496+
497+
class ValueLabelTypeMismatch(Warning):
498+
"""
499+
Warning raised by to_stata on a category column that contains non-string values.
500+
501+
Examples
502+
--------
503+
>>> df = pd.DataFrame({"categories": pd.Series(["a", 2], dtype="category")})
504+
>>> df.to_stata('test') # doctest: +SKIP
505+
... # ValueLabelTypeMismatch: Stata value labels (pandas categories) must be str...
506+
"""
507+
508+
509+
class InvalidColumnName(Warning):
510+
"""
511+
Warning raised by to_stata the column contains a non-valid stata name.
512+
513+
Because the column name is an invalid Stata variable, the name needs to be
514+
converted.
515+
516+
Examples
517+
--------
518+
>>> df = pd.DataFrame({"0categories": pd.Series([2, 2])})
519+
>>> df.to_stata('test') # doctest: +SKIP
520+
... # InvalidColumnName: Not all pandas column names were valid Stata variable...
521+
"""
522+
523+
524+
class CategoricalConversionWarning(Warning):
525+
"""
526+
Warning is raised when reading a partial labeled Stata file using a iterator.
527+
528+
Examples
529+
--------
530+
>>> from pandas.io.stata import StataReader
531+
>>> with StataReader('dta_file', chunksize=2) as reader: # doctest: +SKIP
532+
... for i, block in enumerate(reader):
533+
... print(i, block))
534+
... # CategoricalConversionWarning: One or more series with value labels...
535+
"""
536+
537+
482538
__all__ = [
483539
"AbstractMethodError",
484540
"AccessorRegistrationWarning",
485541
"AttributeConflictWarning",
542+
"CategoricalConversionWarning",
486543
"ClosedFileError",
487544
"CSSWarning",
488545
"DatabaseError",
@@ -492,6 +549,7 @@ class DatabaseError(OSError):
492549
"EmptyDataError",
493550
"IncompatibilityWarning",
494551
"IntCastingNaNError",
552+
"InvalidColumnName",
495553
"InvalidIndexError",
496554
"IndexingError",
497555
"MergeError",
@@ -505,6 +563,7 @@ class DatabaseError(OSError):
505563
"ParserWarning",
506564
"PerformanceWarning",
507565
"PossibleDataLossError",
566+
"PossiblePrecisionLoss",
508567
"PyperclipException",
509568
"PyperclipWindowsException",
510569
"SettingWithCopyError",
@@ -513,4 +572,5 @@ class DatabaseError(OSError):
513572
"UndefinedVariableError",
514573
"UnsortedIndexError",
515574
"UnsupportedFunctionCall",
575+
"ValueLabelTypeMismatch",
516576
]

pandas/io/stata.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
StorageOptions,
4242
WriteBuffer,
4343
)
44+
from pandas.errors import (
45+
CategoricalConversionWarning,
46+
InvalidColumnName,
47+
PossiblePrecisionLoss,
48+
ValueLabelTypeMismatch,
49+
)
4450
from pandas.util._decorators import (
4551
Appender,
4652
doc,
@@ -493,31 +499,19 @@ def g(x: datetime.datetime) -> int:
493499
"""
494500

495501

496-
class PossiblePrecisionLoss(Warning):
497-
pass
498-
499-
500502
precision_loss_doc: Final = """
501503
Column converted from {0} to {1}, and some data are outside of the lossless
502504
conversion range. This may result in a loss of precision in the saved data.
503505
"""
504506

505507

506-
class ValueLabelTypeMismatch(Warning):
507-
pass
508-
509-
510508
value_label_mismatch_doc: Final = """
511509
Stata value labels (pandas categories) must be strings. Column {0} contains
512510
non-string labels which will be converted to strings. Please check that the
513511
Stata data file created has not lost information due to duplicate labels.
514512
"""
515513

516514

517-
class InvalidColumnName(Warning):
518-
pass
519-
520-
521515
invalid_name_doc: Final = """
522516
Not all pandas column names were valid Stata variable names.
523517
The following replacements have been made:
@@ -530,11 +524,7 @@ class InvalidColumnName(Warning):
530524
"""
531525

532526

533-
class CategoricalConversionWarning(Warning):
534-
pass
535-
536-
537-
categorical_conversion_warning = """
527+
categorical_conversion_warning: Final = """
538528
One or more series with value labels are not fully labeled. Reading this
539529
dataset with an iterator results in categorical variable with different
540530
categories. This occurs since it is not possible to know all possible values

pandas/tests/test_errors.py

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"IncompatibilityWarning",
3636
"AttributeConflictWarning",
3737
"DatabaseError",
38+
"PossiblePrecisionLoss",
39+
"CategoricalConversionWarning",
40+
"InvalidColumnName",
41+
"ValueLabelTypeMismatch",
3842
],
3943
)
4044
def test_exception_importable(exc):

0 commit comments

Comments
 (0)