Skip to content

Commit 74a71fe

Browse files
committed
DOC: applied jreback's feedback
PR #21406 - Applied jreback's feedback in PR #21406
1 parent 8bde6c6 commit 74a71fe

File tree

1 file changed

+42
-106
lines changed

1 file changed

+42
-106
lines changed

doc/source/whatsnew/v0.24.0.txt

+42-106
Original file line numberDiff line numberDiff line change
@@ -202,49 +202,32 @@ Backwards incompatible API changes
202202
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203203

204204
:func:`DataFrame.to_csv` now uses :func:`os.linesep` rather than ``'\n'``
205-
for the default line terminator(:issue:`20353`).
206-
- This change only affects when running on Windows, where ``'\r\n'`` was used for line terminator
205+
for the default line terminator (:issue:`20353`).
206+
This change only affects when running on Windows, where ``'\r\n'`` was used for line terminator
207207
even when ``'\n'`` was passed in ``line_terminator``.
208-
- Strictly speeaking, all ``'\n'``s appear in data and line terminator of CSV were converted into ``'\r\n'``s.
209-
- This problem was resolved by passing file object with ``newline='\n'`` option as output, rather than file name.
210208

211209
Previous Behavior on Windows:
212210

213211
.. code-block:: ipython
214212

215-
In [1]: import pandas as pd
213+
In [1]: data = pd.DataFrame({
214+
...: "string_with_lf":["a\nbc"],
215+
...: "string_with_crlf":["a\r\nbc"]
216+
...: })
216217

217-
In [2]: data = pd.DataFrame({
218-
...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
219-
...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
220-
...: })
218+
In [2]: data.to_csv("test.csv",index=False,line_terminator='\n')
221219

222-
In [3]: data.to_csv("test.csv",index=False,line_terminator='\n')
220+
In [3]: with open("test.csv", mode='rb') as f:
221+
...: print(f.read())
222+
b'string_with_lf,string_with_crlf\r\n"a\r\nbc","a\r\r\nbc"\r\n'
223223

224-
In [4]: print(pd.read_csv("test.csv"))
225-
string_with_lf string_with_crlf
226-
0 abc abc
227-
1 d\r\nef d\r\r\nef
228-
2 g\r\nh\r\n\r\ni g\r\r\nh\r\r\n\r\r\ni
224+
In [4]: with open("test2.csv", mode='w', newline='\n') as f:
225+
...: data.to_csv(f,index=False,line_terminator='\n')
229226

230-
In [5]: with open("test.csv", mode='rb') as f:
231-
...: print(f.read())
232-
b'string_with_lf,string_with_crlf\r\nabc,abc\r\n"d\r\nef","d\r\r\nef"\r\n"g\r\nh
233-
\r\n\r\ni","g\r\r\nh\r\r\n\r\r\ni"\r\n'
227+
In [5]: with open("test2.csv", mode='rb') as f:
228+
...: print(f.read())
229+
b'string_with_lf,string_with_crlf\n"a\nbc","a\r\nbc"\n'
234230

235-
In [6]: with open("test2.csv", mode='w', newline='\n') as f:
236-
...: data.to_csv(f,index=False,line_terminator='\n')
237-
238-
In [7]: print(pd.read_csv("test2.csv"))
239-
string_with_lf string_with_crlf
240-
0 abc abc
241-
1 d\nef d\r\nef
242-
2 g\nh\n\ni g\r\nh\r\n\r\ni
243-
244-
In [8]: with open("test2.csv", mode='rb') as f:
245-
...: print(f.read())
246-
b'string_with_lf,string_with_crlf\nabc,abc\n"d\nef","d\r\nef"\n"g\nh\n\ni","g\r\
247-
nh\r\n\r\ni"\n'
248231

249232
New Behavior on Windows:
250233

@@ -254,99 +237,52 @@ New Behavior on Windows:
254237

255238
.. code-block:: ipython
256239

257-
In [1]: import pandas as pd
258-
259-
In [2]: data = pd.DataFrame({
260-
...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
261-
...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
262-
...: })
240+
In [1]: data = pd.DataFrame({
241+
...: "string_with_lf":["a\nbc"],
242+
...: "string_with_crlf":["a\r\nbc"]
243+
...: })
263244

264-
In [3]: data.to_csv("test.csv",index=False,line_terminator='\n')
245+
In [2]: data.to_csv("test.csv",index=False,line_terminator='\n')
265246

266-
In [4]: pd.read_csv("test.csv")
267-
Out[4]:
268-
string_with_lf string_with_crlf
269-
0 abc abc
270-
1 d\nef d\r\nef
271-
2 g\nh\n\ni g\r\nh\r\n\r\ni
247+
In [3]: with open("test.csv", mode='rb') as f:
248+
...: print(f.read())
249+
b'string_with_lf,string_with_crlf\n"a\nbc","a\r\nbc"\n'
272250

273-
In [5]: with open("test.csv", mode='rb') as f:
274-
...: binary_str=f.read()
275-
...: binary_str
276-
Out[5]: b'string_with_lf,string_with_crlf\nabc,abc\n"d\nef","d\r\nef"\n"g\nh\n\n
277-
i","g\r\nh\r\n\r\ni"\n'
278251

279252
- On windows, the value of ``os.linesep`` is ``'\r\n'``,
280253
so if ``line_terminator`` is not set, ``'\r\n'`` is used for line terminator.
281254
- Again, it does not affects the value inside the data.
282255

283256
.. code-block:: ipython
284257

285-
In [1]: import pandas as pd
258+
In [1]: data = pd.DataFrame({
259+
...: "string_with_lf":["a\nbc"],
260+
...: "string_with_crlf":["a\r\nbc"]
261+
...: })
286262

287-
In [2]: data = pd.DataFrame({
288-
...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
289-
...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
290-
...: })
263+
In [2]: data.to_csv("test.csv",index=False)
291264

292-
In [3]: data.to_csv("test.csv",index=False)
265+
In [3]: with open("test.csv", mode='rb') as f:
266+
...: print(f.read())
267+
b'string_with_lf,string_with_crlf\r\n"a\nbc","a\r\nbc"\r\n'
293268

294-
In [4]: pd.read_csv("test.csv")
295-
Out[4]:
296-
string_with_lf string_with_crlf
297-
0 abc abc
298-
1 d\nef d\r\nef
299-
2 g\nh\n\ni g\r\nh\r\n\r\ni
300-
301-
In [5]: with open("test.csv", mode='rb') as f:
302-
...: binary_str=f.read()
303-
...: binary_str
304-
Out[5]: b'string_with_lf,string_with_crlf\r\nabc,abc\r\n"d\nef","d\r\nef"\r\n"g\
305-
nh\n\ni","g\r\nh\r\n\r\ni"\r\n'
306269

307270
- As default value of ``line_terminator`` changes, just passing file object with ``newline='\n'`` does not set ``'\n'`` to line terminator.
308271
Pass ``line_terminator='\n'`` explicitly.
309272

310273
.. code-block:: ipython
311274

312-
In [1]: import pandas as pd
313-
314-
In [2]: data = pd.DataFrame({
315-
...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
316-
...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
317-
...: })
318-
319-
In [3]: with open("test2.csv", mode='w', newline='\n') as f:
320-
...: data.to_csv(f,index=False)
321-
322-
In [4]: pd.read_csv("test2.csv")
323-
Out[4]:
324-
string_with_lf string_with_crlf
325-
0 abc abc
326-
1 d\nef d\r\nef
327-
2 g\nh\n\ni g\r\nh\r\n\r\ni
328-
329-
In [5]: with open("test2.csv", mode='rb') as f:
330-
...: binary_str=f.read()
331-
...: binary_str
332-
Out[5]: b'string_with_lf,string_with_crlf\r\nabc,abc\r\n"d\nef","d\r\nef"\r\n"g\
333-
nh\n\ni","g\r\nh\r\n\r\ni"\r\n'
334-
335-
In [6]: with open("test2.csv", mode='w', newline='\n') as f:
336-
...: data.to_csv(f,index=False,line_terminator='\n')
337-
338-
In [7]: pd.read_csv("test2.csv")
339-
Out[7]:
340-
string_with_lf string_with_crlf
341-
0 abc abc
342-
1 d\nef d\r\nef
343-
2 g\nh\n\ni g\r\nh\r\n\r\ni
344-
345-
In [8]: with open("test2.csv", mode='rb') as f:
346-
...: binary_str=f.read()
347-
...: binary_str
348-
Out[8]: b'string_with_lf,string_with_crlf\nabc,abc\n"d\nef","d\r\nef"\n"g\nh\n\n
349-
i","g\r\nh\r\n\r\ni"\n'
275+
In [1]: data = pd.DataFrame({
276+
...: "string_with_lf":["a\nbc"],
277+
...: "string_with_crlf":["a\r\nbc"]
278+
...: })
279+
280+
In [2]: with open("test2.csv", mode='w', newline='\n') as f:
281+
...: data.to_csv(f,index=False)
282+
283+
In [3]: with open("test2.csv", mode='rb') as f:
284+
...: print(f.read())
285+
b'string_with_lf,string_with_crlf\r\n"a\nbc","a\r\nbc"\r\n'
350286

351287

352288
.. _whatsnew_0240.api_breaking.interval_values:

0 commit comments

Comments
 (0)