@@ -29,8 +29,8 @@ duplicates present. The output can't be determined, and so pandas raises.
29
29
.. ipython :: python
30
30
:okexcept:
31
31
32
- s1 = pd.Series([0 , 1 , 2 ], index = [' a ' , ' b ' , ' b ' ])
33
- s1.reindex([' a ' , ' b ' , ' c ' ])
32
+ s1 = pd.Series([0 , 1 , 2 ], index = [" a " , " b " , " b " ])
33
+ s1.reindex([" a " , " b " , " c " ])
34
34
35
35
Other methods, like indexing, can give very surprising results. Typically
36
36
indexing with a scalar will *reduce dimensionality *. Slicing a ``DataFrame ``
@@ -39,30 +39,30 @@ return a scalar. But with duplicates, this isn't the case.
39
39
40
40
.. ipython :: python
41
41
42
- df1 = pd.DataFrame([[0 , 1 , 2 ], [3 , 4 , 5 ]], columns = [' A ' , ' A ' , ' B ' ])
42
+ df1 = pd.DataFrame([[0 , 1 , 2 ], [3 , 4 , 5 ]], columns = [" A " , " A " , " B " ])
43
43
df1
44
44
45
45
We have duplicates in the columns. If we slice ``'B' ``, we get back a ``Series ``
46
46
47
47
.. ipython :: python
48
48
49
- df1[' B ' ] # a series
49
+ df1[" B " ] # a series
50
50
51
51
But slicing ``'A' `` returns a ``DataFrame ``
52
52
53
53
54
54
.. ipython :: python
55
55
56
- df1[' A ' ] # a DataFrame
56
+ df1[" A " ] # a DataFrame
57
57
58
58
This applies to row labels as well
59
59
60
60
.. ipython :: python
61
61
62
- df2 = pd.DataFrame({" A" : [0 , 1 , 2 ]}, index = [' a ' , ' a ' , ' b ' ])
62
+ df2 = pd.DataFrame({" A" : [0 , 1 , 2 ]}, index = [" a " , " a " , " b " ])
63
63
df2
64
- df2.loc[' b ' , ' A ' ] # a scalar
65
- df2.loc[' a ' , ' A ' ] # a Series
64
+ df2.loc[" b " , " A " ] # a scalar
65
+ df2.loc[" a " , " A " ] # a Series
66
66
67
67
Duplicate Label Detection
68
68
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -121,29 +121,24 @@ will be raised.
121
121
.. ipython :: python
122
122
:okexcept:
123
123
124
- pd.Series(
125
- [0 , 1 , 2 ],
126
- index = [' a' , ' b' , ' b' ]
127
- ).set_flags(allows_duplicate_labels = False )
124
+ pd.Series([0 , 1 , 2 ], index = [" a" , " b" , " b" ]).set_flags(allows_duplicate_labels = False )
128
125
129
126
This applies to both row and column labels for a :class: `DataFrame `
130
127
131
128
.. ipython :: python
132
129
:okexcept:
133
130
134
- pd.DataFrame(
135
- [[ 0 , 1 , 2 ], [ 3 , 4 , 5 ]], columns = [ " A " , " B " , " C " ],
136
- ).set_flags( allows_duplicate_labels = False )
131
+ pd.DataFrame([[ 0 , 1 , 2 ], [ 3 , 4 , 5 ]], columns = [ " A " , " B " , " C " ],).set_flags(
132
+ allows_duplicate_labels = False
133
+ )
137
134
138
135
This attribute can be checked or set with :attr: `~DataFrame.flags.allows_duplicate_labels `,
139
136
which indicates whether that object can have duplicate labels.
140
137
141
138
.. ipython :: python
142
139
143
- df = (
144
- pd.DataFrame({" A" : [0 , 1 , 2 , 3 ]},
145
- index = [' x' , ' y' , ' X' , ' Y' ])
146
- .set_flags(allows_duplicate_labels = False )
140
+ df = pd.DataFrame({" A" : [0 , 1 , 2 , 3 ]}, index = [" x" , " y" , " X" , " Y" ]).set_flags(
141
+ allows_duplicate_labels = False
147
142
)
148
143
df
149
144
df.flags.allows_duplicate_labels
@@ -198,7 +193,7 @@ operations.
198
193
.. ipython :: python
199
194
:okexcept:
200
195
201
- s1 = pd.Series(0 , index = [' a ' , ' b ' ]).set_flags(allows_duplicate_labels = False )
196
+ s1 = pd.Series(0 , index = [" a " , " b " ]).set_flags(allows_duplicate_labels = False )
202
197
s1
203
198
s1.head().rename({" a" : " b" })
204
199
0 commit comments