Skip to content

API: enable set_levels/set_names/set_labels to accept a list and a level argument to change a single level/value #7792

Closed
@Poquaruse

Description

@Poquaruse

e.g.

set_names(self, names, inplace=False, level=None)
set_levels(self, levels, level=None, copy=False, validate=True, verify_integrity=False

if level is not None then treat levels/names as a list (or list-of-lists if level is a list) and just set those levels.

e.g.

set_names('foo',level=1)
set_names(['foo','bar'],level=[1,2])
set_levels(['a','b','c'],level=1)
set_levels([['a','b','c'],[1,2,3]],level=[1,2])

Hi all,

first of all: I'm not sure whether this is a bug or if there is just no nice way to do this. I'd like to use tz_convert in a MultiIndex DataFrame in pandas 0.14.1.

dt_rng = pd.date_range(start='2014-01-01 00:00', periods = 1000, freq='1s', tz='Europe/Berlin')
df = pd.DataFrame({'a':np.random.randn(1000), 'b': np.random.randn(1000)},index = dt_rng)
df['b'] = df['b'].round()
df = df.groupby('b').resample('1h')
df.index.levels[1] = df.index.levels[1].tz_convert('UTC')

This does not work: 'FrozenList' does not support mutable operations.

What I currently do:

dt_rng = pd.date_range(start='2014-01-01 00:00', periods = 1000, freq='1s', tz='Europe/Berlin')
df = pd.DataFrame({'a':np.random.randn(1000), 'b': np.random.randn(1000)},index = dt_rng)
df['b'] = df['b'].round()
df = df.groupby('b').resample('1h')
df.index.set_levels([
    df.index.levels[0],
    df.index.levels[1].tz_convert('UTC')
],inplace=True)

This does work as expected, but is it really the way to do it?!

Thanks and best regards!

PS: INSTALLED VERSIONS

commit: None
python: 3.4.1.final.0
python-bits: 64
OS: Windows
OS-release: 8
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: DE

pandas: 0.14.1
nose: 1.3.3
Cython: 0.20.1
numpy: 1.8.1
scipy: 0.14.0
statsmodels: None
IPython: 2.1.0
sphinx: 1.2.2
patsy: 0.2.1
scikits.timeseries: None
dateutil: 2.1
pytz: 2014.4
bottleneck: None
tables: 3.1.1
numexpr: 2.3.1
matplotlib: 1.3.1
openpyxl: 1.8.5
xlrd: 0.9.3
xlwt: None
xlsxwriter: 0.5.5
lxml: 3.3.5
bs4: 4.3.1
html5lib: None
httplib2: None
apiclient: None
rpy2: None
sqlalchemy: 0.9.4
pymysql: None
psycopg2: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions