本文整理匯總了Python中pandas.to_numeric方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.to_numeric方法的具體用法?Python pandas.to_numeric怎麽用?Python pandas.to_numeric使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.to_numeric方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def __init__(self, code, start=None, end=None):
"""
:param code: str. 指數代碼,eg. SH000016
:param start:
:param end:
"""
df = xu.get_daily("teb-" + code, start=start, end=end)
df["e"] = pd.to_numeric(df["e"])
df["b"] = pd.to_numeric(df["b"])
df["lnb"] = df["b"].apply(lambda s: np.log(s))
df["lne"] = df["e"].apply(lambda s: np.log(s))
df["roe"] = df["e"] / df["b"] * 100
df["date_count"] = (df["date"] - df["date"].iloc[0]).apply(
lambda s: int(s.days)
)
self.df = df
self.fit(verbose=False)
示例2: __init__
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def __init__(
self, code, name=None, start=None, end=None, rate=0, col="close", **kws
):
if not name:
try:
name = get_rt(code)["name"]
except:
name = code
self.name = name
self.code = code
self.start = start # None is one year ago
self.end = end # None is yesterday
df = get_daily(code, start=start, end=end)
df[col] = pd.to_numeric(df[col]) # in case the col is not float
df["totvalue"] = df[col]
df["netvalue"] = df[col] / df.iloc[0][col]
self.price = df
self.round_label = kws.get("round_label", 0)
self.dividend_label = kws.get("dividend_label", 0)
self.value_label = kws.get("value_label", 1) # 默認按金額贖回
self.specialdate = []
self.fenhongdate = []
self.zhesuandate = []
self.rate = rate
示例3: df_fx
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def df_fx(self, currency, fx_provider):
try:
# First get the df from this currency
if currency != 'USD':
fx = PriceData(currency, fx_provider)
fx.df = fx.df.rename(columns={'close': 'fx_close'})
fx.df["fx_close"] = pd.to_numeric(fx.df.fx_close,
errors='coerce')
# Merge the two dfs:
merge_df = pd.merge(self.df, fx.df, on='date', how='inner')
merge_df['close'] = merge_df['close'].astype(float)
merge_df['close_converted'] = merge_df['close'] * merge_df[
'fx_close']
return (merge_df)
else: # If currency is USD no conversion is needed - prices are all in USD
self.df['fx_close'] = 1
self.df['close_converted'] = self.df['close'].astype(float)
return (self.df)
except Exception as e:
self.errors.append(e)
return (None)
示例4: test_error
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_error(self):
s = pd.Series([1, -3.14, 'apple'])
msg = 'Unable to parse string "apple" at position 2'
with pytest.raises(ValueError, match=msg):
to_numeric(s, errors='raise')
res = to_numeric(s, errors='ignore')
expected = pd.Series([1, -3.14, 'apple'])
tm.assert_series_equal(res, expected)
res = to_numeric(s, errors='coerce')
expected = pd.Series([1, -3.14, np.nan])
tm.assert_series_equal(res, expected)
s = pd.Series(['orange', 1, -3.14, 'apple'])
msg = 'Unable to parse string "orange" at position 0'
with pytest.raises(ValueError, match=msg):
to_numeric(s, errors='raise')
示例5: test_numeric_lists_and_arrays
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_numeric_lists_and_arrays(self):
# Test to_numeric with embedded lists and arrays
df = pd.DataFrame(dict(
a=[[decimal.Decimal(3.14), 1.0], decimal.Decimal(1.6), 0.1]
))
df['a'] = df['a'].apply(to_numeric)
expected = pd.DataFrame(dict(
a=[[3.14, 1.0], 1.6, 0.1],
))
tm.assert_frame_equal(df, expected)
df = pd.DataFrame(dict(
a=[np.array([decimal.Decimal(3.14), 1.0]), 0.1]
))
df['a'] = df['a'].apply(to_numeric)
expected = pd.DataFrame(dict(
a=[[3.14, 1.0], 0.1],
))
tm.assert_frame_equal(df, expected)
示例6: test_numeric_dtypes
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_numeric_dtypes(self):
idx = pd.Index([1, 2, 3], name='xxx')
res = pd.to_numeric(idx)
tm.assert_index_equal(res, idx)
res = pd.to_numeric(pd.Series(idx, name='xxx'))
tm.assert_series_equal(res, pd.Series(idx, name='xxx'))
res = pd.to_numeric(idx.values)
tm.assert_numpy_array_equal(res, idx.values)
idx = pd.Index([1., np.nan, 3., np.nan], name='xxx')
res = pd.to_numeric(idx)
tm.assert_index_equal(res, idx)
res = pd.to_numeric(pd.Series(idx, name='xxx'))
tm.assert_series_equal(res, pd.Series(idx, name='xxx'))
res = pd.to_numeric(idx.values)
tm.assert_numpy_array_equal(res, idx.values)
示例7: test_str
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_str(self):
idx = pd.Index(['1', '2', '3'], name='xxx')
exp = np.array([1, 2, 3], dtype='int64')
res = pd.to_numeric(idx)
tm.assert_index_equal(res, pd.Index(exp, name='xxx'))
res = pd.to_numeric(pd.Series(idx, name='xxx'))
tm.assert_series_equal(res, pd.Series(exp, name='xxx'))
res = pd.to_numeric(idx.values)
tm.assert_numpy_array_equal(res, exp)
idx = pd.Index(['1.5', '2.7', '3.4'], name='xxx')
exp = np.array([1.5, 2.7, 3.4])
res = pd.to_numeric(idx)
tm.assert_index_equal(res, pd.Index(exp, name='xxx'))
res = pd.to_numeric(pd.Series(idx, name='xxx'))
tm.assert_series_equal(res, pd.Series(exp, name='xxx'))
res = pd.to_numeric(idx.values)
tm.assert_numpy_array_equal(res, exp)
示例8: test_coerce_uint64_conflict
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_coerce_uint64_conflict(self):
# see gh-17007 and gh-17125
#
# Still returns float despite the uint64-nan conflict,
# which would normally force the casting to object.
df = pd.DataFrame({"a": [200, 300, "", "NaN", 30000000000000000000]})
expected = pd.Series([200, 300, np.nan, np.nan,
30000000000000000000], dtype=float, name="a")
result = to_numeric(df["a"], errors="coerce")
tm.assert_series_equal(result, expected)
s = pd.Series(["12345678901234567890", "1234567890", "ITEM"])
expected = pd.Series([12345678901234567890,
1234567890, np.nan], dtype=float)
result = to_numeric(s, errors="coerce")
tm.assert_series_equal(result, expected)
# For completeness, check against "ignore" and "raise"
result = to_numeric(s, errors="ignore")
tm.assert_series_equal(result, s)
msg = "Unable to parse string"
with pytest.raises(ValueError, match=msg):
to_numeric(s, errors="raise")
示例9: test_metadata_no_nan
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_metadata_no_nan(self, mock_etl2, mock_open):
edges = triangleEdges.copy()
edges['testNone'] = triangleNodes.a1.map(lambda x: numpy.nan)
edges['testNone'] = pandas.to_numeric(edges.testNone, errors='ignore')
edges['testInt'] = triangleNodes.a1.map(lambda x: numpy.nan if x%2 == 1 else 0)
edges['testFloat'] = triangleNodes.a1.map(lambda x: numpy.nan if x%2 == 1 else 0.5)
edges['testString'] = triangleNodes.a1.map(lambda x: numpy.nan if x%2 == 1 else 'foo')
edges['testBool'] = triangleNodes.a1.map(lambda x: numpy.nan if x%2 == 1 else True)
graphistry.bind(source='src', destination='dst', node='id').plot(edges)
dataset = mock_etl2.call_args[0][0]
#for attrib in ['testInt', 'testFloat', 'testString', 'testBool', 'testNone']:
# for entry in list(dataset['attributes']['edges'][attrib]['aggregations'].values()):
# if entry is None or isinstance(entry, str):
# pass
# else:
# self.assertFalse(numpy.isnan(entry))
示例10: _sanitize_dataset
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def _sanitize_dataset(self, edges, nodes, nodeid):
self._check_bound_attribs(edges, ['source', 'destination'], 'Edge')
elist = edges.reset_index(drop=True) \
.dropna(subset=[self._source, self._destination])
obj_df = elist.select_dtypes(include=[numpy.object_])
elist[obj_df.columns] = obj_df.apply(pandas.to_numeric, errors='ignore')
if nodes is None:
nodes = pandas.DataFrame()
nodes[nodeid] = pandas.concat([edges[self._source], edges[self._destination]],
ignore_index=True).drop_duplicates()
else:
self._check_bound_attribs(nodes, ['node'], 'Vertex')
nlist = nodes.reset_index(drop=True) \
.dropna(subset=[nodeid]) \
.drop_duplicates(subset=[nodeid])
obj_df = nlist.select_dtypes(include=[numpy.object_])
nlist[obj_df.columns] = obj_df.apply(pandas.to_numeric, errors='ignore')
return (elist, nlist)
示例11: get_user_specified_features
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def get_user_specified_features(df, featurizer, verbose=True):
"""Extract and merge user specified features.
Merge features included in dataset provided by user
into final features dataframe
Three types of featurization here:
1) Molecule featurization
-) Smiles string featurization
-) Rdkit MOL featurization
2) Complex featurization
-) PDB files for interacting molecules.
3) User specified featurizations.
"""
time1 = time.time()
df[featurizer.feature_fields] = df[featurizer.feature_fields].apply(
pd.to_numeric)
X_shard = df.as_matrix(columns=featurizer.feature_fields)
time2 = time.time()
log("TIMING: user specified processing took %0.3f s" % (time2 - time1),
verbose)
return X_shard
示例12: test_error
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_error(self):
s = pd.Series([1, -3.14, 'apple'])
msg = 'Unable to parse string "apple" at position 2'
with tm.assert_raises_regex(ValueError, msg):
to_numeric(s, errors='raise')
res = to_numeric(s, errors='ignore')
expected = pd.Series([1, -3.14, 'apple'])
tm.assert_series_equal(res, expected)
res = to_numeric(s, errors='coerce')
expected = pd.Series([1, -3.14, np.nan])
tm.assert_series_equal(res, expected)
s = pd.Series(['orange', 1, -3.14, 'apple'])
msg = 'Unable to parse string "orange" at position 0'
with tm.assert_raises_regex(ValueError, msg):
to_numeric(s, errors='raise')
示例13: test_coerce_uint64_conflict
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def test_coerce_uint64_conflict(self):
# see gh-17007 and gh-17125
#
# Still returns float despite the uint64-nan conflict,
# which would normally force the casting to object.
df = pd.DataFrame({"a": [200, 300, "", "NaN", 30000000000000000000]})
expected = pd.Series([200, 300, np.nan, np.nan,
30000000000000000000], dtype=float, name="a")
result = to_numeric(df["a"], errors="coerce")
tm.assert_series_equal(result, expected)
s = pd.Series(["12345678901234567890", "1234567890", "ITEM"])
expected = pd.Series([12345678901234567890,
1234567890, np.nan], dtype=float)
result = to_numeric(s, errors="coerce")
tm.assert_series_equal(result, expected)
# For completeness, check against "ignore" and "raise"
result = to_numeric(s, errors="ignore")
tm.assert_series_equal(result, s)
msg = "Unable to parse string"
with tm.assert_raises_regex(ValueError, msg):
to_numeric(s, errors="raise")
示例14: load_metrics_from_db
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def load_metrics_from_db(db_path, tx_mode, aln_mode):
"""
Loads the alignment metrics for the mRNA/CDS alignments of transMap/AugustusTM/TMR
"""
session = tools.sqlInterface.start_session(db_path)
metrics_table = tools.sqlInterface.tables[aln_mode][tx_mode]['metrics']
metrics_df = tools.sqlInterface.load_metrics(metrics_table, session)
# unstack flattens the long-form data structure
metrics_df = metrics_df.set_index(['AlignmentId', 'classifier']).unstack('classifier')
metrics_df.columns = [col[1] for col in metrics_df.columns]
metrics_df = metrics_df.reset_index()
cols = ['AlnCoverage', 'AlnGoodness', 'AlnIdentity', 'PercentUnknownBases']
metrics_df[cols] = metrics_df[cols].apply(pd.to_numeric)
metrics_df['OriginalIntrons'] = metrics_df['OriginalIntrons'].fillna('')
metrics_df['OriginalIntrons'] = [list(map(int, x)) if len(x[0]) > 0 else [] for x in
metrics_df['OriginalIntrons'].str.split(',').tolist()]
metrics_df['OriginalIntronsPercent'] = metrics_df['OriginalIntrons'].apply(calculate_vector_support, resolve_nan=1)
session.close()
return metrics_df
示例15: _format_series_data
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import to_numeric [as 別名]
def _format_series_data(data_series):
"""
The purpose of this function is to convert the series data into a rasterizeable
format if possible.
Parameters
----------
data_series: :obj:`geopandas.GeoSeries`
The series to be converted.
Returns
-------
:obj:`geopandas.GeoSeries`: The series that was converted if possible.
"""
if "datetime" in str(data_series.dtype):
data_series = pandas.to_numeric(data_series).astype(numpy.float64)
get_logger().warning(
f"The series '{data_series.name}' was converted from a date to a number to "
"rasterize the data. To load the data back in as a date, "
"use 'pandas.to_datetime()'."
)
elif str(data_series.dtype) == "category":
data_series = data_series.cat.codes
return data_series