File tree Expand file tree Collapse file tree 3 files changed +41
-2
lines changed Expand file tree Collapse file tree 3 files changed +41
-2
lines changed Original file line number Diff line number Diff line change
1
+ cbook.is_sequence_of_strings recognizes string objects
2
+ ``````````````````````````````````````````````````````
3
+
4
+ This is primarily how pandas stores a sequence of strings.
5
+
6
+ import pandas as pd
7
+ import matplotlib.cbook as cbook
8
+
9
+ a = np.array(['a', 'b', 'c'])
10
+ print(cbook.is_sequence_of_strings(a)) # True
11
+
12
+ a = np.array(['a', 'b', 'c'], dtype=object)
13
+ print(cbook.is_sequence_of_strings(a)) # True
14
+
15
+ s = pd.Series(['a', 'b', 'c'])
16
+ print(cbook.is_sequence_of_strings(s)) # True
17
+
18
+ Previously, the last two prints returned false.
Original file line number Diff line number Diff line change @@ -783,8 +783,12 @@ def is_sequence_of_strings(obj):
783
783
"""
784
784
if not iterable (obj ):
785
785
return False
786
- if is_string_like (obj ):
787
- return False
786
+ if is_string_like (obj ) and not isinstance (obj , np .ndarray ):
787
+ try :
788
+ obj = obj .values
789
+ except AttributeError :
790
+ # not pandas
791
+ return False
788
792
for o in obj :
789
793
if not is_string_like (o ):
790
794
return False
Original file line number Diff line number Diff line change @@ -26,6 +26,23 @@ def test_is_string_like():
26
26
assert cbook .is_string_like ("hello world" )
27
27
assert_equal (cbook .is_string_like (10 ), False )
28
28
29
+ y = ['a' , 'b' , 'c' ]
30
+ assert_equal (cbook .is_string_like (y ), False )
31
+
32
+ y = np .array (y )
33
+ assert_equal (cbook .is_string_like (y ), False )
34
+
35
+ y = np .array (y , dtype = object )
36
+ assert cbook .is_string_like (y )
37
+
38
+
39
+ def test_is_sequence_of_strings ():
40
+ y = ['a' , 'b' , 'c' ]
41
+ assert cbook .is_sequence_of_strings (y )
42
+
43
+ y = np .array (y , dtype = object )
44
+ assert cbook .is_sequence_of_strings (y )
45
+
29
46
30
47
def test_restrict_dict ():
31
48
d = {'foo' : 'bar' , 1 : 2 }
You can’t perform that action at this time.
0 commit comments