Skip to content

Commit 2e32ba3

Browse files
committed
Update See Also. Add more examples and specifies return type
1 parent 78f342c commit 2e32ba3

File tree

2 files changed

+137
-6
lines changed

2 files changed

+137
-6
lines changed

Pandas Documentation Day.ipynb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [
10+
{
11+
"ename": "ImportError",
12+
"evalue": "C extension: numpy.core.multiarray failed to import not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.",
13+
"output_type": "error",
14+
"traceback": [
15+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
16+
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
17+
"\u001b[0;32m__init__.pxd\u001b[0m in \u001b[0;36mnumpy.import_array\u001b[0;34m()\u001b[0m\n",
18+
"\u001b[0;31mRuntimeError\u001b[0m: module compiled against API version 0xb but this version of numpy is 0xa",
19+
"\nDuring handling of the above exception, another exception occurred:\n",
20+
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
21+
"\u001b[0;32m/home/aaron/projects/pandas/pandas/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m from pandas._libs import (hashtable as _hashtable,\n\u001b[0m\u001b[1;32m 27\u001b[0m \u001b[0mlib\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0m_lib\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
22+
"\u001b[0;32m/home/aaron/projects/pandas/pandas/_libs/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mtslib\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0miNaT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNaT\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTimestamp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTimedelta\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mOutOfBoundsDatetime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
23+
"\u001b[0;32m/home/aaron/projects/pandas/pandas/_libs/tslibs/conversion.pxd\u001b[0m in \u001b[0;36minit pandas._libs.tslib\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mcdef\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0m_TSObject\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mcdef\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
24+
"\u001b[0;32m/home/aaron/projects/pandas/pandas/_libs/tslibs/conversion.pyx\u001b[0m in \u001b[0;36minit pandas._libs.tslibs.conversion\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;31m# -*- coding: utf-8 -*-\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m# cython: profile=False\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
25+
"\u001b[0;32m/home/aaron/projects/pandas/pandas/_libs/tslibs/nattype.pyx\u001b[0m in \u001b[0;36minit pandas._libs.tslibs.nattype\u001b[0;34m()\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0mcimport\u001b[0m \u001b[0mint64_t\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mcnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimport_array\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
26+
"\u001b[0;32m__init__.pxd\u001b[0m in \u001b[0;36mnumpy.import_array\u001b[0;34m()\u001b[0m\n",
27+
"\u001b[0;31mImportError\u001b[0m: numpy.core.multiarray failed to import",
28+
"\nDuring handling of the above exception, another exception occurred:\n",
29+
"\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
30+
"\u001b[0;32m<ipython-input-1-a9be8501a591>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mpandas\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
31+
"\u001b[0;32m/home/aaron/projects/pandas/pandas/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;34m\"pandas from the source directory, you may need to run \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;34m\"'python setup.py build_ext --inplace --force' to build \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m \"the C extensions first.\".format(module))\n\u001b[0m\u001b[1;32m 36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mdatetime\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
32+
"\u001b[0;31mImportError\u001b[0m: C extension: numpy.core.multiarray failed to import not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first."
33+
]
34+
}
35+
],
36+
"source": [
37+
"import pandas as pd\n",
38+
"df = pd.DataFrame([[1,2,3], [4,5,6], [7,8,9]])"
39+
]
40+
}
41+
],
42+
"metadata": {
43+
"kernelspec": {
44+
"display_name": "Python 3",
45+
"language": "python",
46+
"name": "python3"
47+
}
48+
},
49+
"nbformat": 4,
50+
"nbformat_minor": 2
51+
}

pandas/core/indexing.py

+86-6
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,10 @@ class _LocIndexer(_LocationIndexer):
14381438
--------
14391439
DateFrame.at
14401440
Access a single value for a row/column label pair
1441-
DateFrame.iat
1442-
Access a single value for a row/column pair by integer position
14431441
DateFrame.iloc
14441442
Access group of rows and columns by integer position(s)
1443+
Series.loc
1444+
Access group of values using labels
14451445
14461446
Examples
14471447
--------
@@ -1453,17 +1453,21 @@ class _LocIndexer(_LocationIndexer):
14531453
r1 0 4 1
14541454
r2 10 20 30
14551455
1456-
Single label for row (note it would be faster to use ``DateFrame.at`` in
1457-
this case)
1456+
Single label. Note this returns a Series.
14581457
14591458
>>> df.loc['r1']
14601459
c0 0
14611460
c1 4
14621461
c2 1
14631462
Name: r1, dtype: int64
14641463
1465-
Single label for row and column (note it would be faster to use
1466-
``DateFrame.at`` in this case)
1464+
List with a single label. Note using ``[[]]`` returns a DataFrame.
1465+
1466+
>>> df.loc[['r1']]
1467+
c0 c1 c2
1468+
r1 0 4 1
1469+
1470+
Single label for row and column
14671471
14681472
>>> df.loc['r0', 'c1']
14691473
2
@@ -1557,6 +1561,82 @@ class _LocIndexer(_LocationIndexer):
15571561
8 0 4 1
15581562
9 10 20 30
15591563
1564+
A number of examples using a DataFrame with a multi-index
1565+
1566+
>>> tuples = [('r0', 'bar'), ('r0', 'foo'), ('r1', 'bar'),
1567+
... ('r1', 'foo'), ('r2', 'bar'), ('r2', 'baz')]
1568+
>>> index = pd.MultiIndex.from_tuples(tuples)
1569+
>>> values = [[12,2,3], [0,4,1], [10,20,30],
1570+
... [1, 4, 1], [7, 1, 2], [16, 36, 40]]
1571+
>>> df = pd.DataFrame(values, columns=['c0', 'c1', 'c2'], index=index)
1572+
>>> df
1573+
c0 c1 c2
1574+
r0 bar 12 2 3
1575+
foo 0 4 1
1576+
r1 bar 10 20 30
1577+
foo 1 4 1
1578+
r2 bar 7 1 2
1579+
baz 16 36 40
1580+
1581+
Single label. Note this returns a DataFrame with a single index.
1582+
1583+
>>> df.loc['r0']
1584+
c0 c1 c2
1585+
bar 12 2 3
1586+
foo 0 4 1
1587+
1588+
Single index tuple. Note this returns a Series.
1589+
1590+
>>> df.loc[('r0', 'bar')]
1591+
c0 12
1592+
c1 2
1593+
c2 3
1594+
Name: (r0, bar), dtype: int64
1595+
1596+
Single label for row and column. Similar to passing in a tuple, this
1597+
returns a Series.
1598+
1599+
>>> df.loc['r0', 'foo']
1600+
c0 0
1601+
c1 4
1602+
c2 1
1603+
Name: (r0, foo), dtype: int64
1604+
1605+
Single tuple. Note using ``[[]]`` returns a DataFrame.
1606+
1607+
>>> df.loc[[('r0', 'bar')]]
1608+
c0 c1 c2
1609+
r0 bar 12 2 3
1610+
1611+
Single tuple for the index with a single label for the column
1612+
1613+
>>> df.loc[('r0', 'foo'), 'c1']
1614+
4
1615+
1616+
Boolean list
1617+
1618+
>>> df.loc[[True, False, True, False, True, True]]
1619+
c0 c1 c2
1620+
r0 bar 12 2 3
1621+
r1 bar 10 20 30
1622+
r2 bar 7 1 2
1623+
baz 16 36 40
1624+
1625+
Slice from index tuple to single label
1626+
1627+
>>> df.loc[('r0', 'foo'):'r1']
1628+
c0 c1 c2
1629+
r0 foo 0 4 1
1630+
r1 bar 10 20 30
1631+
foo 1 4 1
1632+
1633+
Slice from index tuple to index tuple
1634+
1635+
>>> df.loc[('r0', 'foo'):('r1', 'bar')]
1636+
c0 c1 c2
1637+
r0 foo 0 4 1
1638+
r1 bar 10 20 30
1639+
15601640
Raises
15611641
------
15621642
KeyError:

0 commit comments

Comments
 (0)