Python pandas 模块,RangeIndex() 实例源码
我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用pandas.RangeIndex()。
def setattributeindex(self, instance, value):
bus_name = instance.bus.index
instance.branch['F_BUS'] = instance.branch['F_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
instance.branch['T_BUS'] = instance.branch['T_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
instance.gen['GEN_BUS'] = instance.gen['GEN_BUS'].apply(lambda x: value[bus_name.get_loc(x)])
try:
instance.load.columns = [v for b, v in zip(instance.bus_name.isin(instance.load.columns), value) if b == True]
except ValueError:
instance.load.columns = value
except AttributeError:
instance.load = pd.DataFrame(0, index=range(0, 1), columns=value, dtype='float')
instance.bus.index = value
if isinstance(instance.bus_name, pd.RangeIndex) or isinstance(instance.bus_name, pd.Int64Index):
logger.debug('Forcing string types for all bus names')
instance.bus_name = ['Bus{}'.format(b) for b in instance.bus_name]
def test_check_exact_times_true(self):
self.pm.check_timestamp(3600, exact_times=True)
expected = pd.DataFrame(
array([['', '', Timestamp('2016-10-17 02:05:00'),
Timestamp('2016-10-17 03:05:00'), 2,
'Missing timestamp']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=1, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def test_check_exact_times_false(self):
self.pm.check_timestamp(3600, exact_times=False)
expected = pd.DataFrame(
array([['', '', Timestamp('2016-10-17 02:00:00'),
Timestamp('2016-10-17 02:00:00'), 1, 'Missing timestamp']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=1, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def test_check_exact_times_true_with_start_time(self):
self.pm.check_timestamp(3600, expected_start_time=Timestamp('2016-10-17 01:00:00'), exact_times=True)
expected = pd.DataFrame(
array([['', '', Timestamp('2016-10-17 01:00:00'),
Timestamp('2016-10-17 03:00:00'), 3,
'Missing timestamp']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=1, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def test_deadsensor(self):
# dead sensor = < 1 in 5 hours
self.pm.check_delta([1, None], window=5*3600+1, absolute_value=True)
expected = pd.DataFrame(
array([['Test', 'A', Timestamp('2017-01-01 00:00:00'), Timestamp('2017-01-01 05:00:00'), 6, '|Delta| < lower bound, 1'],
['Test', 'A', Timestamp('2017-01-01 16:00:00'), Timestamp('2017-01-01 23:00:00'), 8, '|Delta| < lower bound, 1']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=2, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def test_abrupt_change(self):
# abrupt change = > 7 in 3 hours
self.pm.check_delta([None, 7], window=3*3600+1, absolute_value=True)
expected = pd.DataFrame(
array([['Test', 'A', Timestamp('2017-01-01 13:00:00'), Timestamp('2017-01-01 16:00:00'), 4, '|Delta| > upper bound, 7'],
['Test', 'B', Timestamp('2017-01-01 10:00:00'), Timestamp('2017-01-01 12:00:00'), 3, '|Delta| > upper bound, 7'],
['Test', 'B', Timestamp('2017-01-01 16:00:00'), Timestamp('2017-01-01 19:00:00'), 4, '|Delta| > upper bound, 7']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=3, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def test_abrupt_negative_change(self):
# abrupt negative change = < -7 in 3 hours
self.pm.check_delta([-7, None], window=3*3600+1, absolute_value=False)
expected = pd.DataFrame(
array([['Test', 'B', Timestamp('2017-01-01 10:00:00'), Timestamp('2017-01-01 12:00:00'), 3, 'Delta < lower bound, -7']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=1, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def test_outlier(self):
# outlier if stdev > 1.9
self.pm.check_outlier([-1.9, 1.9], window=None, absolute_value=False)
expected = pd.DataFrame(
array([['Test', 'A', Timestamp('2017-01-01 19:00:00'), Timestamp('2017-01-01 19:00:00'), 1, 'Outlier < lower bound, -1.9'],
['Test', 'A', Timestamp('2017-01-01 06:00:00'), Timestamp('2017-01-01 06:00:00'), 1, 'Outlier > upper bound, 1.9']], dtype=object),
columns=['System Name', 'Variable Name', 'Start Date', 'End Date', 'Timesteps', 'Error Flag'],
index=RangeIndex(start=0, stop=2, step=1)
)
assert_frame_equal(expected, self.pm.test_results)
def transform(self, dataframe, display_schema):
csv_df = self._format_columns(dataframe, display_schema['metrics'], display_schema['dimensions'])
if isinstance(dataframe.index, pd.RangeIndex):
# If there are no dimensions, just serialize to csv without the index
return csv_df.to_csv(index=False)
csv_df = self._format_index(csv_df, display_schema['dimensions'])
row_dimension_labels = self._format_row_dimension_labels(display_schema['dimensions'])
return csv_df.to_csv(index_label=row_dimension_labels)
def calc_ladder(scores_df, year=2016):
'''
DataFrame with championship ladder with round-robin games for the given year.
Wins, draws and losses are worth 4, 2 and 0 points respectively.
'''
# Select a subset of the rows
# df.loc[] matches dates as strings like '20160506' or '2016'.
# Note here rounds are simple strings so sort with R1 < R10 < R2 < .. < R9
# (we could change this with a CategoricalIndex)
# Note also that pandas 0.18.0 has a bug with .loc on MultiIndexes
# if dates are the first level. It works as expected if we
# move the dates to the end before slicing
scores2 = scores_df.reorder_levels([1, 2, 3, 0]).sort_index()
x = scores2.loc(axis=0)[:, 'R1':'R9', :, str(year):str(year)]
# Don't need to put levels back in order as we are about to drop 3 of them
# x = x.reorder_levels([3, 0, 1, 2]).sort_index()
# Just keep Team. This does a copy too, avoiding SettingWithCopy warning
y = x.reset_index(['Date', 'Venue', 'Round'], drop=True)
# Add cols with 0/1 for number of games played, won, drawn and lost
y['P'] = 1
y['W'] = (y['F'] > y['A']).astype(int)
y['D'] = 0
y.loc[y['F'] == y['A'], 'D'] = 1
y.eval('L = 1*(A>F)', inplace=True)
print(y)
# Subtotal by team and then sort by Points/Percentage
t = y.groupby(level='Team').sum()
t['PCT'] = 100.0 * t.F / t.A
t['PTS'] = 4 * t['W'] + 2 * t['D']
ladder = t.sort_values(['PTS', 'PCT'], ascending=False)
# Add ladder position (note: assumes no ties!)
ladder['Pos'] = pd.RangeIndex(1, len(ladder) + 1)
print(ladder)
return ladder
def setattributeindex(self, instance, value):
instance.gen.index = value
instance.gencost.index = value
if isinstance(instance.gen_name, pd.RangeIndex) or isinstance(instance.bus_name, pd.Int64Index):
instance.gen_name = ['GenCo{}'.format(g) for g in instance.gen_name]
def make_meta(x):
"""Create an empty pygdf object containing the desired metadata.
Parameters
----------
x : dict, tuple, list, pd.Series, pd.DataFrame, pd.Index, dtype, scalar
To create a DataFrame, provide a `dict` mapping of `{name: dtype}`, or
an iterable of `(name, dtype)` tuples. To create a `Series`, provide a
tuple of `(name, dtype)`. If a pygdf object, names, dtypes, and index
should match the desired output. If a dtype or scalar, a scalar of the
same dtype is returned.
Examples
--------
>>> make_meta([('a', 'i8'), ('b', 'O')])
Empty DataFrame
Columns: [a, b]
Index: []
>>> make_meta(('a', 'f8'))
Series([], Name: a, dtype: float64)
>>> make_meta('i8')
1
"""
if hasattr(x, '_meta'):
return x._meta
if isinstance(x, (gd.Series, gd.DataFrame, gd.index.Index)):
out = x[:2]
return out.copy() if hasattr(out, 'copy') else out
meta = dd.utils.make_meta(x)
if isinstance(meta, (pd.DataFrame, pd.Series, pd.Index)):
meta2 = dd.utils.meta_nonempty(meta)
if isinstance(meta2, pd.DataFrame):
return gd.DataFrame.from_pandas(meta2)
elif isinstance(meta2, pd.Series):
return gd.Series.from_any(meta2)
else:
if isinstance(meta2, pd.RangeIndex):
return gd.index.RangeIndex(meta2.start, meta2.stop)
return gd.index.GenericIndex(meta2)
return meta