def test_object_array_to_fixed_string(self):
# Ticket #1235.
a = np.array(['abcdefgh', 'ijklmnop'], dtype=np.object_)
b = np.array(a, dtype=(np.str_, 8))
assert_equal(a, b)
c = np.array(a, dtype=(np.str_, 5))
assert_equal(c, np.array(['abcde', 'ijklm']))
d = np.array(a, dtype=(np.str_, 12))
assert_equal(a, d)
e = np.empty((2, ), dtype=(np.str_, 8))
e[:] = a[:]
assert_equal(a, e)
python类object_()的实例源码
def test_split(self):
A = self.A.split(asbytes('3'))
tgt = asbytes_nested([
[[' abc '], ['']],
[['12', '45'], ['MixedCase']],
[['12', ' \t ', '45 \x00 '], ['UPPER']]])
assert_(issubclass(A.dtype.type, np.object_))
assert_equal(A.tolist(), tgt)
def test_splitlines(self):
A = np.char.array(['abc\nfds\nwer']).splitlines()
assert_(issubclass(A.dtype.type, np.object_))
assert_(A.shape == (1,))
assert_(len(A[0]) == 3)
def test_converters_cornercases(self):
# Test the conversion to datetime.
converter = {
'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')}
data = TextIO('2009-02-03 12:00:00Z, 72214.0')
test = np.ndfromtxt(data, delimiter=',', dtype=None,
names=['date', 'stid'], converters=converter)
control = np.array((datetime(2009, 2, 3), 72214.),
dtype=[('date', np.object_), ('stid', float)])
assert_equal(test, control)
def test_dtype_error(self):
for f in self.nanfuncs:
for dtype in [np.bool_, np.int_, np.object_]:
assert_raises(TypeError, f, _ndat, axis=1, dtype=dtype)
def test_out_dtype_error(self):
for f in self.nanfuncs:
for dtype in [np.bool_, np.int_, np.object_]:
out = np.empty(_ndat.shape[0], dtype=dtype)
assert_raises(TypeError, f, _ndat, axis=1, out=out)
def split_df_col2cols(dataframe, split_col, split_char, new_colnames, delete_old = False):
# # Splits a column into multiple columns
# dataframe : pandas dataframe to be processed
# split_col : chr string of the column name to be split
# split_char : chr to split the col on
# new_colnames : list of new name for the columns
# delete_old : logical True / False, remove original column?
# ~~~~~~~~~~~~~~~~ #
import pandas as pd
import numpy as np
# pl.my_debugger(globals().copy())
# my_debugger(locals().copy())
# save the split column as a separate object
new_cols = dataframe[split_col].astype(np.object_).str.split(split_char).apply(pd.Series, 1)
# if all values were NaN, no split occured, only one col exists still
if len(new_cols.columns) < len(new_colnames):
# create the missing cols, fill with NaN
for i in range(len(new_cols.columns), len(new_colnames)):
new_cols[new_colnames[i]] = np.nan
# rename the cols
new_cols.columns = new_colnames
# remove the original column from the df
if delete_old is True:
del dataframe[split_col]
# merge with df
new_df = dataframe.join(new_cols)
return new_df
def split_df_col2cols(dataframe, split_col, split_char, new_colnames, delete_old = False):
# # Splits a column into multiple columns
# dataframe : pandas dataframe to be processed
# split_col : chr string of the column name to be split
# split_char : chr to split the col on
# new_colnames : list of new name for the columns
# delete_old : logical True / False, remove original column?
# ~~~~~~~~~~~~~~~~ #
import pandas as pd
import numpy as np
# pl.my_debugger(globals().copy())
# my_debugger(locals().copy())
# save the split column as a separate object
new_cols = dataframe[split_col].astype(np.object_).str.split(split_char).apply(pd.Series, 1)
# if all values were NaN, no split occured, only one col exists still
if len(new_cols.columns) < len(new_colnames):
# create the missing cols, fill with NaN
for i in range(len(new_cols.columns), len(new_colnames)):
new_cols[new_colnames[i]] = np.nan
# rename the cols
new_cols.columns = new_colnames
# remove the original column from the df
if delete_old is True:
del dataframe[split_col]
# merge with df
new_df = dataframe.join(new_cols)
return new_df
def split_df_col2cols(dataframe, split_col, split_char, new_colnames, delete_old = False):
# # Splits a column into multiple columns
# dataframe : pandas dataframe to be processed
# split_col : chr string of the column name to be split
# split_char : chr to split the col on
# new_colnames : list of new name for the columns
# delete_old : logical True / False, remove original column?
# ~~~~~~~~~~~~~~~~ #
import pandas as pd
import numpy as np
# pl.my_debugger(globals().copy())
# my_debugger(locals().copy())
# save the split column as a separate object
new_cols = dataframe[split_col].astype(np.object_).str.split(split_char).apply(pd.Series, 1)
# if all values were NaN, no split occured, only one col exists still
if len(new_cols.columns) < len(new_colnames):
# create the missing cols, fill with NaN
for i in range(len(new_cols.columns), len(new_colnames)):
new_cols[new_colnames[i]] = np.nan
# rename the cols
new_cols.columns = new_colnames
# remove the original column from the df
if delete_old is True:
del dataframe[split_col]
# merge with df
new_df = dataframe.join(new_cols)
return new_df
def split_df_col2cols(dataframe, split_col, split_char, new_colnames, delete_old = False):
# # Splits a column into multiple columns
# dataframe : pandas dataframe to be processed
# split_col : chr string of the column name to be split
# split_char : chr to split the col on
# new_colnames : list of new name for the columns
# delete_old : logical True / False, remove original column?
# ~~~~~~~~~~~~~~~~ #
import pandas as pd
import numpy as np
# pl.my_debugger(globals().copy())
# my_debugger(locals().copy())
# save the split column as a separate object
new_cols = dataframe[split_col].astype(np.object_).str.split(split_char).apply(pd.Series, 1)
# if all values were NaN, no split occured, only one col exists still
if len(new_cols.columns) < len(new_colnames):
# create the missing cols, fill with NaN
for i in range(len(new_cols.columns), len(new_colnames)):
new_cols[new_colnames[i]] = np.nan
# rename the cols
new_cols.columns = new_colnames
# remove the original column from the df
if delete_old is True:
del dataframe[split_col]
# merge with df
new_df = dataframe.join(new_cols)
return new_df
def split_df_col2cols(dataframe, split_col, split_char, new_colnames, delete_old = False):
# # Splits a column into multiple columns
# dataframe : pandas dataframe to be processed
# split_col : chr string of the column name to be split
# split_char : chr to split the col on
# new_colnames : list of new name for the columns
# delete_old : logical True / False, remove original column?
# ~~~~~~~~~~~~~~~~ #
import pandas as pd
import numpy as np
# pl.my_debugger(globals().copy())
# my_debugger(locals().copy())
# save the split column as a separate object
new_cols = dataframe[split_col].astype(np.object_).str.split(split_char).apply(pd.Series, 1)
# if all values were NaN, no split occured, only one col exists still
if len(new_cols.columns) < len(new_colnames):
# create the missing cols, fill with NaN
for i in range(len(new_cols.columns), len(new_colnames)):
new_cols[new_colnames[i]] = np.nan
# rename the cols
new_cols.columns = new_colnames
# remove the original column from the df
if delete_old is True:
del dataframe[split_col]
# merge with df
new_df = dataframe.join(new_cols)
return new_df
def test_unpickle_dtype_with_object(self,level=rlevel):
# Implemented in r2840
dt = np.dtype([('x', int), ('y', np.object_), ('z', 'O')])
f = BytesIO()
pickle.dump(dt, f)
f.seek(0)
dt_ = pickle.load(f)
f.close()
assert_equal(dt, dt_)
def test_mem_array_creation_invalid_specification(self,level=rlevel):
# Ticket #196
dt = np.dtype([('x', int), ('y', np.object_)])
# Wrong way
self.assertRaises(ValueError, np.array, [1, 'object'], dt)
# Correct way
np.array([(1, 'object')], dt)
def test_for_object_scalar_creation(self, level=rlevel):
# Ticket #816
a = np.object_()
b = np.object_(3)
b2 = np.object_(3.0)
c = np.object_([4, 5])
d = np.object_([None, {}, []])
assert_(a is None)
assert_(type(b) is int)
assert_(type(b2) is float)
assert_(type(c) is np.ndarray)
assert_(c.dtype == object)
assert_(d.dtype == object)
def test_object_array_to_fixed_string(self):
# Ticket #1235.
a = np.array(['abcdefgh', 'ijklmnop'], dtype=np.object_)
b = np.array(a, dtype=(np.str_, 8))
assert_equal(a, b)
c = np.array(a, dtype=(np.str_, 5))
assert_equal(c, np.array(['abcde', 'ijklm']))
d = np.array(a, dtype=(np.str_, 12))
assert_equal(a, d)
e = np.empty((2, ), dtype=(np.str_, 8))
e[:] = a[:]
assert_equal(a, e)
def test_split(self):
A = self.A.split(asbytes('3'))
tgt = asbytes_nested([
[[' abc '], ['']],
[['12', '45'], ['MixedCase']],
[['12', ' \t ', '45 \x00 '], ['UPPER']]])
assert_(issubclass(A.dtype.type, np.object_))
assert_equal(A.tolist(), tgt)
def test_splitlines(self):
A = np.char.array(['abc\nfds\nwer']).splitlines()
assert_(issubclass(A.dtype.type, np.object_))
assert_(A.shape == (1,))
assert_(len(A[0]) == 3)
def test_converters_cornercases(self):
# Test the conversion to datetime.
converter = {
'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')}
data = TextIO('2009-02-03 12:00:00Z, 72214.0')
test = np.ndfromtxt(data, delimiter=',', dtype=None,
names=['date', 'stid'], converters=converter)
control = np.array((datetime(2009, 2, 3), 72214.),
dtype=[('date', np.object_), ('stid', float)])
assert_equal(test, control)
def test_dtype_error(self):
for f in self.nanfuncs:
for dtype in [np.bool_, np.int_, np.object_]:
assert_raises(TypeError, f, _ndat, axis=1, dtype=dtype)
def test_out_dtype_error(self):
for f in self.nanfuncs:
for dtype in [np.bool_, np.int_, np.object_]:
out = np.empty(_ndat.shape[0], dtype=dtype)
assert_raises(TypeError, f, _ndat, axis=1, out=out)