Skip to content

Commit c5c5c11

Browse files
committed
TST: updated expected values for some tests
* Updates: * Updated expected values for some tests about 'to_csv()' method, to deal with new default value of 'line_terminator' arg. * Related Issue: * Issue pandas-dev#20353 * PR pandas-dev#21406
1 parent 7e89b8d commit c5c5c11

File tree

2 files changed

+111
-74
lines changed

2 files changed

+111
-74
lines changed

pandas/tests/frame/test_to_csv.py

+48-40
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,10 @@ def test_to_csv_unicodewriter_quoting(self):
842842
encoding='utf-8')
843843

844844
result = buf.getvalue()
845-
expected = ('"A","B"\n'
846-
'1,"foo"\n'
847-
'2,"bar"\n'
848-
'3,"baz"\n')
845+
expected = ('"A","B"' + os.linesep +
846+
'1,"foo"' + os.linesep +
847+
'2,"bar"' + os.linesep +
848+
'3,"baz"' + os.linesep)
849849

850850
assert result == expected
851851

@@ -857,7 +857,9 @@ def test_to_csv_quote_none(self):
857857
df.to_csv(buf, quoting=csv.QUOTE_NONE,
858858
encoding=encoding, index=False)
859859
result = buf.getvalue()
860-
expected = 'A\nhello\n{"hello"}\n'
860+
expected = ('A' + os.linesep +
861+
'hello' + os.linesep +
862+
'{"hello"}' + os.linesep)
861863
assert result == expected
862864

863865
def test_to_csv_index_no_leading_comma(self):
@@ -866,10 +868,10 @@ def test_to_csv_index_no_leading_comma(self):
866868

867869
buf = StringIO()
868870
df.to_csv(buf, index_label=False)
869-
expected = ('A,B\n'
870-
'one,1,4\n'
871-
'two,2,5\n'
872-
'three,3,6\n')
871+
expected = ('A,B' + os.linesep +
872+
'one,1,4' + os.linesep +
873+
'two,2,5' + os.linesep +
874+
'three,3,6' + os.linesep)
873875
assert buf.getvalue() == expected
874876

875877
def test_to_csv_line_terminators(self):
@@ -1072,11 +1074,9 @@ def test_to_csv_quoting(self):
10721074
'c_string': ['a', 'b,c'],
10731075
})
10741076

1075-
expected = """\
1076-
,c_bool,c_float,c_int,c_string
1077-
0,True,1.0,42.0,a
1078-
1,False,3.2,,"b,c"
1079-
"""
1077+
expected = (',c_bool,c_float,c_int,c_string' + os.linesep +
1078+
'0,True,1.0,42.0,a' + os.linesep +
1079+
'1,False,3.2,,"b,c"' + os.linesep)
10801080
result = df.to_csv()
10811081
assert result == expected
10821082

@@ -1086,21 +1086,19 @@ def test_to_csv_quoting(self):
10861086
result = df.to_csv(quoting=csv.QUOTE_MINIMAL)
10871087
assert result == expected
10881088

1089-
expected = """\
1090-
"","c_bool","c_float","c_int","c_string"
1091-
"0","True","1.0","42.0","a"
1092-
"1","False","3.2","","b,c"
1093-
"""
1089+
expected = ('"","c_bool","c_float","c_int","c_string"' + os.linesep +
1090+
'"0","True","1.0","42.0","a"' + os.linesep +
1091+
'"1","False","3.2","","b,c"' + os.linesep)
1092+
10941093
result = df.to_csv(quoting=csv.QUOTE_ALL)
10951094
assert result == expected
10961095

10971096
# see gh-12922, gh-13259: make sure changes to
10981097
# the formatters do not break this behaviour
1099-
expected = """\
1100-
"","c_bool","c_float","c_int","c_string"
1101-
0,True,1.0,42.0,"a"
1102-
1,False,3.2,"","b,c"
1103-
"""
1098+
expected = ('"","c_bool","c_float","c_int","c_string"' + os.linesep +
1099+
'0,True,1.0,42.0,"a"' + os.linesep +
1100+
'1,False,3.2,"","b,c"' + os.linesep)
1101+
11041102
result = df.to_csv(quoting=csv.QUOTE_NONNUMERIC)
11051103
assert result == expected
11061104

@@ -1111,27 +1109,24 @@ def test_to_csv_quoting(self):
11111109
quoting=csv.QUOTE_NONE,
11121110
escapechar=None)
11131111

1114-
expected = """\
1115-
,c_bool,c_float,c_int,c_string
1116-
0,True,1.0,42.0,a
1117-
1,False,3.2,,b!,c
1118-
"""
1112+
expected = (',c_bool,c_float,c_int,c_string' + os.linesep +
1113+
'0,True,1.0,42.0,a' + os.linesep +
1114+
'1,False,3.2,,b!,c' + os.linesep)
11191115
result = df.to_csv(quoting=csv.QUOTE_NONE,
11201116
escapechar='!')
11211117
assert result == expected
11221118

1123-
expected = """\
1124-
,c_bool,c_ffloat,c_int,c_string
1125-
0,True,1.0,42.0,a
1126-
1,False,3.2,,bf,c
1127-
"""
1119+
expected = (',c_bool,c_ffloat,c_int,c_string' + os.linesep +
1120+
'0,True,1.0,42.0,a' + os.linesep +
1121+
'1,False,3.2,,bf,c' + os.linesep)
11281122
result = df.to_csv(quoting=csv.QUOTE_NONE,
11291123
escapechar='f')
11301124
assert result == expected
11311125

11321126
# see gh-3503: quoting Windows line terminators
11331127
# presents with encoding?
1134-
text = 'a,b,c\n1,"test \r\n",3\n'
1128+
text = ('a,b,c' + os.linesep +
1129+
'1,"test \r\n",3' + os.linesep)
11351130
df = pd.read_csv(StringIO(text))
11361131
buf = StringIO()
11371132
df.to_csv(buf, encoding='utf-8', index=False)
@@ -1141,7 +1136,9 @@ def test_to_csv_quoting(self):
11411136
# with multi-indexes
11421137
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]})
11431138
df = df.set_index(['a', 'b'])
1144-
expected = '"a","b","c"\n"1","3","5"\n"2","4","6"\n'
1139+
expected = ('"a","b","c"' + os.linesep +
1140+
'"1","3","5"' + os.linesep +
1141+
'"2","4","6"' + os.linesep)
11451142
assert df.to_csv(quoting=csv.QUOTE_ALL) == expected
11461143

11471144
def test_period_index_date_overflow(self):
@@ -1153,13 +1150,19 @@ def test_period_index_date_overflow(self):
11531150
df = pd.DataFrame([4, 5, 6], index=index)
11541151
result = df.to_csv()
11551152

1156-
expected = ',0\n1990-01-01,4\n2000-01-01,5\n3005-01-01,6\n'
1153+
expected = (',0' + os.linesep +
1154+
'1990-01-01,4' + os.linesep +
1155+
'2000-01-01,5' + os.linesep +
1156+
'3005-01-01,6' + os.linesep)
11571157
assert result == expected
11581158

11591159
date_format = "%m-%d-%Y"
11601160
result = df.to_csv(date_format=date_format)
11611161

1162-
expected = ',0\n01-01-1990,4\n01-01-2000,5\n01-01-3005,6\n'
1162+
expected = (',0' + os.linesep +
1163+
'01-01-1990,4' + os.linesep +
1164+
'01-01-2000,5' + os.linesep +
1165+
'01-01-3005,6' + os.linesep)
11631166
assert result == expected
11641167

11651168
# Overflow with pd.NaT
@@ -1169,7 +1172,10 @@ def test_period_index_date_overflow(self):
11691172
df = pd.DataFrame([4, 5, 6], index=index)
11701173
result = df.to_csv()
11711174

1172-
expected = ',0\n1990-01-01,4\n,5\n3005-01-01,6\n'
1175+
expected = (',0' + os.linesep +
1176+
'1990-01-01,4' + os.linesep +
1177+
',5' + os.linesep +
1178+
'3005-01-01,6' + os.linesep)
11731179
assert result == expected
11741180

11751181
def test_multi_index_header(self):
@@ -1182,5 +1188,7 @@ def test_multi_index_header(self):
11821188
header = ["a", "b", "c", "d"]
11831189
result = df.to_csv(header=header)
11841190

1185-
expected = ",a,b,c,d\n0,1,2,3,4\n1,5,6,7,8\n"
1191+
expected = (',a,b,c,d' + os.linesep +
1192+
'0,1,2,3,4' + os.linesep +
1193+
'1,5,6,7,8' + os.linesep)
11861194
assert result == expected

pandas/tests/io/formats/test_to_csv.py

+63-34
Original file line numberDiff line numberDiff line change
@@ -130,29 +130,37 @@ def test_to_csv_escapechar(self):
130130

131131
def test_csv_to_string(self):
132132
df = DataFrame({'col': [1, 2]})
133-
expected = ',col\n0,1\n1,2\n'
133+
expected = (',col' + os.linesep +
134+
'0,1' + os.linesep +
135+
'1,2' + os.linesep)
134136
assert df.to_csv() == expected
135137

136138
def test_to_csv_decimal(self):
137139
# GH 781
138140
df = DataFrame({'col1': [1], 'col2': ['a'], 'col3': [10.1]})
139141

140-
expected_default = ',col1,col2,col3\n0,1,a,10.1\n'
142+
expected_default = (',col1,col2,col3' + os.linesep +
143+
'0,1,a,10.1' + os.linesep)
141144
assert df.to_csv() == expected_default
142145

143-
expected_european_excel = ';col1;col2;col3\n0;1;a;10,1\n'
146+
expected_european_excel = (';col1;col2;col3' + os.linesep +
147+
'0;1;a;10,1' + os.linesep)
144148
assert df.to_csv(decimal=',', sep=';') == expected_european_excel
145149

146-
expected_float_format_default = ',col1,col2,col3\n0,1,a,10.10\n'
150+
expected_float_format_default = (',col1,col2,col3' + os.linesep +
151+
'0,1,a,10.10' + os.linesep)
147152
assert df.to_csv(float_format='%.2f') == expected_float_format_default
148153

149-
expected_float_format = ';col1;col2;col3\n0;1;a;10,10\n'
154+
expected_float_format = (';col1;col2;col3' + os.linesep +
155+
'0;1;a;10,10' + os.linesep)
150156
assert df.to_csv(decimal=',', sep=';',
151157
float_format='%.2f') == expected_float_format
152158

153159
# GH 11553: testing if decimal is taken into account for '0.0'
154160
df = pd.DataFrame({'a': [0, 1.1], 'b': [2.2, 3.3], 'c': 1})
155-
expected = 'a,b,c\n0^0,2^2,1\n1^1,3^3,1\n'
161+
expected = ('a,b,c' + os.linesep +
162+
'0^0,2^2,1' + os.linesep +
163+
'1^1,3^3,1' + os.linesep)
156164
assert df.to_csv(index=False, decimal='^') == expected
157165

158166
# same but for an index
@@ -165,7 +173,9 @@ def test_to_csv_float_format(self):
165173
# testing if float_format is taken into account for the index
166174
# GH 11553
167175
df = pd.DataFrame({'a': [0, 1], 'b': [2.2, 3.3], 'c': 1})
168-
expected = 'a,b,c\n0,2.20,1\n1,3.30,1\n'
176+
expected = ('a,b,c' + os.linesep +
177+
'0,2.20,1' + os.linesep +
178+
'1,3.30,1' + os.linesep)
169179
assert df.set_index('a').to_csv(float_format='%.2f') == expected
170180

171181
# same for a multi-index
@@ -176,19 +186,25 @@ def test_to_csv_na_rep(self):
176186
# testing if NaN values are correctly represented in the index
177187
# GH 11553
178188
df = DataFrame({'a': [0, np.NaN], 'b': [0, 1], 'c': [2, 3]})
179-
expected = "a,b,c\n0.0,0,2\n_,1,3\n"
189+
expected = ('a,b,c' + os.linesep +
190+
'0.0,0,2' + os.linesep +
191+
'_,1,3' + os.linesep)
180192
assert df.set_index('a').to_csv(na_rep='_') == expected
181193
assert df.set_index(['a', 'b']).to_csv(na_rep='_') == expected
182194

183195
# now with an index containing only NaNs
184196
df = DataFrame({'a': np.NaN, 'b': [0, 1], 'c': [2, 3]})
185-
expected = "a,b,c\n_,0,2\n_,1,3\n"
197+
expected = ('a,b,c' + os.linesep +
198+
'_,0,2' + os.linesep +
199+
'_,1,3' + os.linesep)
186200
assert df.set_index('a').to_csv(na_rep='_') == expected
187201
assert df.set_index(['a', 'b']).to_csv(na_rep='_') == expected
188202

189203
# check if na_rep parameter does not break anything when no NaN
190204
df = DataFrame({'a': 0, 'b': [0, 1], 'c': [2, 3]})
191-
expected = "a,b,c\n0,0,2\n0,1,3\n"
205+
expected = ('a,b,c' + os.linesep +
206+
'0,0,2' + os.linesep +
207+
'0,1,3' + os.linesep)
192208
assert df.set_index('a').to_csv(na_rep='_') == expected
193209
assert df.set_index(['a', 'b']).to_csv(na_rep='_') == expected
194210

@@ -199,33 +215,46 @@ def test_to_csv_date_format(self):
199215
df_day = DataFrame({'A': pd.date_range('20130101', periods=5, freq='d')
200216
})
201217

202-
expected_default_sec = (',A\n0,2013-01-01 00:00:00\n1,'
203-
'2013-01-01 00:00:01\n2,2013-01-01 00:00:02'
204-
'\n3,2013-01-01 00:00:03\n4,'
205-
'2013-01-01 00:00:04\n')
218+
expected_default_sec = (',A' + os.linesep +
219+
'0,2013-01-01 00:00:00' + os.linesep +
220+
'1,2013-01-01 00:00:01' + os.linesep +
221+
'2,2013-01-01 00:00:02' + os.linesep +
222+
'3,2013-01-01 00:00:03' + os.linesep +
223+
'4,2013-01-01 00:00:04' + os.linesep)
206224
assert df_sec.to_csv() == expected_default_sec
207225

208-
expected_ymdhms_day = (',A\n0,2013-01-01 00:00:00\n1,'
209-
'2013-01-02 00:00:00\n2,2013-01-03 00:00:00'
210-
'\n3,2013-01-04 00:00:00\n4,'
211-
'2013-01-05 00:00:00\n')
226+
expected_ymdhms_day = (',A' + os.linesep +
227+
'0,2013-01-01 00:00:00' + os.linesep +
228+
'1,2013-01-02 00:00:00' + os.linesep +
229+
'2,2013-01-03 00:00:00' + os.linesep +
230+
'3,2013-01-04 00:00:00' + os.linesep +
231+
'4,2013-01-05 00:00:00' + os.linesep)
212232
assert (df_day.to_csv(date_format='%Y-%m-%d %H:%M:%S') ==
213233
expected_ymdhms_day)
214234

215-
expected_ymd_sec = (',A\n0,2013-01-01\n1,2013-01-01\n2,'
216-
'2013-01-01\n3,2013-01-01\n4,2013-01-01\n')
235+
expected_ymd_sec = (',A' + os.linesep +
236+
'0,2013-01-01' + os.linesep +
237+
'1,2013-01-01' + os.linesep +
238+
'2,2013-01-01' + os.linesep +
239+
'3,2013-01-01' + os.linesep +
240+
'4,2013-01-01' + os.linesep)
217241
assert df_sec.to_csv(date_format='%Y-%m-%d') == expected_ymd_sec
218242

219-
expected_default_day = (',A\n0,2013-01-01\n1,2013-01-02\n2,'
220-
'2013-01-03\n3,2013-01-04\n4,2013-01-05\n')
243+
expected_default_day = (',A' + os.linesep +
244+
'0,2013-01-01' + os.linesep +
245+
'1,2013-01-02' + os.linesep +
246+
'2,2013-01-03' + os.linesep +
247+
'3,2013-01-04' + os.linesep +
248+
'4,2013-01-05' + os.linesep)
221249
assert df_day.to_csv() == expected_default_day
222250
assert df_day.to_csv(date_format='%Y-%m-%d') == expected_default_day
223251

224252
# testing if date_format parameter is taken into account for
225253
# multi-indexed dataframes (GH 7791)
226254
df_sec['B'] = 0
227255
df_sec['C'] = 1
228-
expected_ymd_sec = 'A,B,C\n2013-01-01,0,1\n'
256+
expected_ymd_sec = ('A,B,C' + os.linesep +
257+
'2013-01-01,0,1' + os.linesep)
229258
df_sec_grouped = df_sec.groupby([pd.Grouper(key='A', freq='1h'), 'B'])
230259
assert (df_sec_grouped.mean().to_csv(date_format='%Y-%m-%d') ==
231260
expected_ymd_sec)
@@ -234,28 +263,30 @@ def test_to_csv_multi_index(self):
234263
# GH 6618
235264
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]))
236265

237-
exp = ",1\n,2\n0,1\n"
266+
exp = (',1' + os.linesep +
267+
',2' + os.linesep +
268+
'0,1' + os.linesep)
238269
assert df.to_csv() == exp
239270

240-
exp = "1\n2\n1\n"
271+
exp = ('1' + os.linesep + '2' + os.linesep + '1' + os.linesep)
241272
assert df.to_csv(index=False) == exp
242273

243274
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]),
244275
index=pd.MultiIndex.from_arrays([[1], [2]]))
245276

246-
exp = ",,1\n,,2\n1,2,1\n"
277+
exp = (',,1' + os.linesep + ',,2' + os.linesep + '1,2,1' + os.linesep)
247278
assert df.to_csv() == exp
248279

249-
exp = "1\n2\n1\n"
280+
exp = ('1' + os.linesep + '2' + os.linesep + '1' + os.linesep)
250281
assert df.to_csv(index=False) == exp
251282

252283
df = DataFrame(
253284
[1], columns=pd.MultiIndex.from_arrays([['foo'], ['bar']]))
254285

255-
exp = ",foo\n,bar\n0,1\n"
286+
exp = (',foo' + os.linesep + ',bar' + os.linesep + '0,1' + os.linesep)
256287
assert df.to_csv() == exp
257288

258-
exp = "foo\nbar\n1\n"
289+
exp = ('foo' + os.linesep + 'bar' + os.linesep + '1' + os.linesep)
259290
assert df.to_csv(index=False) == exp
260291

261292
def test_to_csv_string_array_ascii(self):
@@ -381,11 +412,9 @@ def test_to_csv_stdout_file(self):
381412
# GH 21561
382413
df = pd.DataFrame([['foo', 'bar'], ['baz', 'qux']],
383414
columns=['name_1', 'name_2'])
384-
expected_ascii = '''\
385-
,name_1,name_2
386-
0,foo,bar
387-
1,baz,qux
388-
'''
415+
expected_ascii = (',name_1,name_2' + os.linesep +
416+
'0,foo,bar' + os.linesep +
417+
'1,baz,qux' + os.linesep)
389418
df.to_csv(sys.stdout, encoding='ascii')
390419
output = sys.stdout.getvalue()
391420
assert output == expected_ascii

0 commit comments

Comments
 (0)