Skip to content

Commit 9507df5

Browse files
committed
Remove usages of deprecated API
1 parent e0dfae9 commit 9507df5

File tree

4 files changed

+29
-19
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/io
    • test/kotlin/org/jetbrains/kotlinx/dataframe/io
  • docs/StardustDocs/topics
  • tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api

4 files changed

+29
-19
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/csv.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public class CSV(private val delimiter: Char = ',') : SupportedDataFrameFormat {
5757
}
5858

5959
public enum class CSVType(public val format: CSVFormat) {
60-
DEFAULT(CSVFormat.DEFAULT.withAllowMissingColumnNames().withIgnoreSurroundingSpaces()),
61-
TDF(CSVFormat.TDF.withAllowMissingColumnNames())
60+
DEFAULT(CSVFormat.DEFAULT.builder().setAllowMissingColumnNames(true).setIgnoreSurroundingSpaces(true).build()),
61+
TDF(CSVFormat.TDF.builder().setAllowMissingColumnNames(true).build())
6262
}
6363

6464
private val defaultCharset = Charsets.UTF_8
@@ -77,7 +77,7 @@ public fun DataFrame.Companion.readDelimStr(
7777
skipLines: Int = 0,
7878
readLines: Int? = null,
7979
): DataFrame<*> =
80-
StringReader(text).use { readDelim(it, CSVType.DEFAULT.format.withHeader(), colTypes, skipLines, readLines) }
80+
StringReader(text).use { readDelim(it, CSVType.DEFAULT.format.builder().setHeader().build(), colTypes, skipLines, readLines) }
8181

8282
public fun DataFrame.Companion.read(
8383
fileOrUrl: String,
@@ -212,7 +212,7 @@ public fun asURL(fileOrUrl: String): URL = (
212212
).toURL()
213213

214214
private fun getFormat(type: CSVType, delimiter: Char, header: List<String>, duplicate: Boolean): CSVFormat =
215-
type.format.withDelimiter(delimiter).withHeader(*header.toTypedArray()).withAllowDuplicateHeaderNames(duplicate)
215+
type.format.builder().setDelimiter(delimiter).setHeader(*header.toTypedArray()).setAllowMissingColumnNames(duplicate).build()
216216

217217
public fun DataFrame.Companion.readDelim(
218218
inStream: InputStream,
@@ -268,7 +268,7 @@ public fun ColType.toType(): KClass<out Any> = when (this) {
268268

269269
public fun DataFrame.Companion.readDelim(
270270
reader: Reader,
271-
format: CSVFormat = CSVFormat.DEFAULT.withHeader(),
271+
format: CSVFormat = CSVFormat.DEFAULT.builder().setHeader().build(),
272272
colTypes: Map<String, ColType> = mapOf(),
273273
skipLines: Int = 0,
274274
readLines: Int? = null,

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/CsvTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class CsvTests {
242242
)
243243
df.writeCSV(
244244
"src/test/resources/without_header.csv",
245-
CSVFormat.DEFAULT.withSkipHeaderRecord(),
245+
CSVFormat.DEFAULT.builder().setSkipHeaderRecord(true).build(),
246246
)
247247
val producedFile = File("src/test/resources/without_header.csv")
248248
producedFile.exists() shouldBe true

docs/StardustDocs/topics/write.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ df.writeCSV(file)
2121
<!---FUN writeCsvStr-->
2222

2323
```kotlin
24-
val csvStr = df.toCsv(CSVFormat.DEFAULT.withDelimiter(';').withRecordSeparator(System.lineSeparator()))
24+
val format = CSVFormat.DEFAULT.builder().setDelimiter(';').setRecordSeparator(System.lineSeparator()).build()
25+
val csvStr = df.toCsv(format)
2526
```
2627

2728
<!---END-->
@@ -104,8 +105,10 @@ val wb = WorkbookFactory.create(true)
104105

105106
// Create different sheets from different data frames in the workbook
106107
val allPersonsSheet = df.writeExcel(wb, sheetName = "allPersons")
107-
val happyPersonsSheet = df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "happyPersons")
108-
val unhappyPersonsSheet = df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "unhappyPersons")
108+
val happyPersonsSheet =
109+
df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "happyPersons")
110+
val unhappyPersonsSheet =
111+
df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "unhappyPersons")
109112

110113
// Do anything you want by POI
111114
listOf(happyPersonsSheet, unhappyPersonsSheet).forEach { setStyles(it) }
@@ -125,9 +128,11 @@ Add new sheets without using Apache POI directly by using a parameter to keep us
125128
// Create a new Excel workbook with a single sheet called "allPersons", replacing the file if it already exists -> Current sheets: allPersons
126129
df.writeExcel(file, sheetName = "allPersons")
127130
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons
128-
df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "happyPersons", keepFile = true)
131+
df.filter { person -> person.isHappy }.remove("isHappy")
132+
.writeExcel(file, sheetName = "happyPersons", keepFile = true)
129133
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons, unhappyPersons
130-
df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "unhappyPersons", keepFile = true)
134+
df.filter { person -> !person.isHappy }.remove("isHappy")
135+
.writeExcel(file, sheetName = "unhappyPersons", keepFile = true)
131136
```
132137

133138
<!---END-->
@@ -203,7 +208,7 @@ df.arrowWriter(
203208
// Specify mismatch subscriber
204209
mismatchSubscriber = writeMismatchMessage,
205210

206-
).use { writer: ArrowWriter ->
211+
).use { writer: ArrowWriter ->
207212

208213
// Save to any format and sink, like in the previous example
209214
writer.writeArrowFeather(file)

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/samples/api/Write.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class Write : TestBase() {
4646
@Test
4747
fun writeCsvStr() {
4848
// SampleStart
49-
val csvStr = df.toCsv(CSVFormat.DEFAULT.withDelimiter(';').withRecordSeparator(System.lineSeparator()))
49+
val format = CSVFormat.DEFAULT.builder().setDelimiter(';').setRecordSeparator(System.lineSeparator()).build()
50+
val csvStr = df.toCsv(format)
5051
// SampleEnd
5152
csvStr shouldStartWith """
5253
name;age;city;weight;isHappy
@@ -116,8 +117,10 @@ class Write : TestBase() {
116117

117118
// Create different sheets from different data frames in the workbook
118119
val allPersonsSheet = df.writeExcel(wb, sheetName = "allPersons")
119-
val happyPersonsSheet = df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "happyPersons")
120-
val unhappyPersonsSheet = df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "unhappyPersons")
120+
val happyPersonsSheet =
121+
df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "happyPersons")
122+
val unhappyPersonsSheet =
123+
df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(wb, sheetName = "unhappyPersons")
121124

122125
// Do anything you want by POI
123126
listOf(happyPersonsSheet, unhappyPersonsSheet).forEach { setStyles(it) }
@@ -153,7 +156,7 @@ class Write : TestBase() {
153156
fun writeArrowPerSchema() {
154157
useTempFile { file ->
155158
val schemaJson =
156-
"""{
159+
"""{
157160
"fields" : [ {
158161
"name" : "name",
159162
"nullable" : true,
@@ -210,7 +213,7 @@ class Write : TestBase() {
210213
// Specify mismatch subscriber
211214
mismatchSubscriber = writeMismatchMessage,
212215

213-
).use { writer: ArrowWriter ->
216+
).use { writer: ArrowWriter ->
214217

215218
// Save to any format and sink, like in the previous example
216219
writer.writeArrowFeather(file)
@@ -226,9 +229,11 @@ class Write : TestBase() {
226229
// Create a new Excel workbook with a single sheet called "allPersons", replacing the file if it already exists -> Current sheets: allPersons
227230
df.writeExcel(file, sheetName = "allPersons")
228231
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons
229-
df.filter { person -> person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "happyPersons", keepFile = true)
232+
df.filter { person -> person.isHappy }.remove("isHappy")
233+
.writeExcel(file, sheetName = "happyPersons", keepFile = true)
230234
// Add a new sheet to the previous file without replacing it, by setting keepFile = true -> Current sheets: allPersons, happyPersons, unhappyPersons
231-
df.filter { person -> !person.isHappy }.remove("isHappy").writeExcel(file, sheetName = "unhappyPersons", keepFile = true)
235+
df.filter { person -> !person.isHappy }.remove("isHappy")
236+
.writeExcel(file, sheetName = "unhappyPersons", keepFile = true)
232237
// SampleEnd
233238
}
234239
}

0 commit comments

Comments
 (0)