Skip to content

Instantly share code, notes, and snippets.

@unutbu
Created October 11, 2014 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unutbu/f1856ae84e6e54195a70 to your computer and use it in GitHub Desktop.
Save unutbu/f1856ae84e6e54195a70 to your computer and use it in GitHub Desktop.
import warnings
import operator
import itertools as IT
import numpy as np
from numpy import nan
import pandas as pd
pd.options.display.width = 1000
pd.options.display.max_rows = 1000
def comparisons():
ops = [
operator.eq,
np.equal,
]
items = [1,
np.float64(1),
np.int32(1),
list(),
[1,2],
[nan, 2],
np.array([], dtype='float'),
np.array([], dtype='int'),
np.array([], dtype='O'),
np.array([nan], dtype='float'),
np.array([nan], dtype='O'),
np.array([nan, 2], dtype='O'),
np.array(['foo'], dtype='O'),
np.array(['foo', 'bar'], dtype='O'),
np.array([1, 2], dtype='O'),
np.array([1, 2], dtype='int'),
np.array([1, 2], dtype='float'),
np.array(['foo', 'bar', 'baz'], dtype='O'),
np.array([1, 2], dtype='float'),
np.array([1, 2], dtype='int'),
np.array(['a', 'b', nan, 'd'], dtype=object),
np.array(['2000-01-02T19:00'], dtype='datetime64[ns]'),
# pd.Series([], dtype='O', index=[]), # index dtype object
# pd.Series([], dtype='O'), # index dtype int64
# pd.Series([], dtype='float'),
# pd.Series([1], dtype='O'),
# pd.Series([1], dtype='float'),
# pd.Series([1, nan], dtype='O'),
# pd.Series([1, nan], dtype='float'),
# pd.Index([1, 2, nan], dtype='O'),
# pd.Index([1, 2, nan], dtype='int64'),
]
result = list()
for (left, right), op in IT.product(IT.combinations_with_replacement(items, 2), ops):
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
val = op(left, right)
except Exception as err:
val = type(err).__name__
with warnings.catch_warnings():
warnings.filterwarnings("error", category=DeprecationWarning)
warnings.filterwarnings("error", category=FutureWarning)
try:
op(left, right)
error = None
msg = None
except Exception as err:
error = type(err).__name__
msg = str(err)
try:
broadcastable = bool(np.broadcast(left, right))
except ValueError:
broadcastable = False
try:
same_shape = left.shape == right.shape
except AttributeError:
same_shape = False
# try:
# same_dtype = left.dtype == right.dtype
# except AttributeError:
# same_dtype = np.asarray(left).dtype == np.asarray(right).dtype
# # same_dtype = False
left_array = isinstance(left, np.ndarray)
right_array = isinstance(right, np.ndarray)
# Use reprs because the objects cause hashing problems when using isin
result.append((repr(left), repr(right),
op.__name__,
# left_array, right_array,
repr(val), str(error), str(msg),
broadcastable,
same_shape,
))
columns = ['left', 'right', 'op',
# 'left_array', 'right_array',
'val', 'error', 'msg',
'broadcastable',
'same_shape',]
df = pd.DataFrame(result, columns=columns)
return df
df = comparisons()
print(df.loc[(df['error']=='DeprecationWarning')][['left','right','op','val', 'error']])
print(df.loc[(df['error']=='FutureWarning')][['left','right','op','val', 'error']])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment