You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Repo Assist] Add DateOnly/TimeOnly inference support (closes#1461) (#1609)
* Add DateOnly/TimeOnly inference support (closes#1461)
- Multi-target library projects to netstandard2.0;net8.0 to enable
NET6_0_OR_GREATER conditional compilation
- Add AsDateOnly/AsTimeOnly to TextConversions with guards against
datetime strings being accepted by DateOnly/TimeOnly parsers
- Add DateOnly to automatic type inference (matchValue); TimeOnly is
NOT auto-inferred (ambiguous with TimeSpan) but available via explicit
schema annotation (dateonly?/timeonly? in CSV headers)
- Add DateOnly as subtype of DateTime in conversionTable; columns with
mixed DateOnly+DateTime values unify to DateTime
- Add ConvertDateOnly/ConvertTimeOnly to TextRuntime and JsonRuntime
- Add DateOnly/TimeOnly cases to StructuralTypes, ConversionsGenerator,
JsonConversionsGenerator, JsonConversions, JsonRuntime, CsvInference
- Update tests for new DateOnly inference behaviour
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Initial plan
* Update documentation and samples for DateOnly/TimeOnly inference
Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com>
* Initial plan
* Fix DateOnly design-time type resolution error for netstandard2.0 targets
Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com>
* apply fantomas formatting
* make optional
* fix tests
* fix tests
* fix tests
---------
Co-authored-by: Repo Assist <repo-assist@github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dsyme <7204669+dsyme@users.noreply.github.com>
Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
Co-authored-by: Don Syme <dsyme@github.com>
// Look at the most recent row. Note the 'Date' property
92
-
// is of type 'DateTime' and 'Open' has a type 'decimal'
92
+
// is of type 'DateTime' by default. Set PreferDateOnly = true
93
+
// to use 'DateOnly' on .NET 6+
94
+
// and 'Open' has a type 'decimal'
93
95
letfirstRow= msft.Rows |> Seq.head
94
96
letlastDate= firstRow.Date
95
97
letlastOpen= firstRow.Open
@@ -107,8 +109,9 @@ collection of rows. We iterate over the rows using a `for` loop. As you can see
107
109
to the columns in the CSV file.
108
110
109
111
As you can see, the type provider also infers types of individual rows. The `Date`
110
-
property is inferred to be a `DateTime` (because the values in the sample file can all
111
-
be parsed as dates) while HLOC prices are inferred as `decimal`.
112
+
property is inferred as `DateTime` by default. When you set `PreferDateOnly = true`
113
+
on .NET 6 and later, date-only strings (without a time component) are inferred as `DateOnly`.
114
+
HLOC prices are inferred as `decimal`.
112
115
113
116
## Using units of measure
114
117
@@ -269,8 +272,12 @@ it by specifying the `InferRows` static parameter of `CsvProvider`. If you speci
269
272
Columns with only `0`, `1`, `Yes`, `No`, `True`, or `False` will be set to `bool`. Columns with numerical values
270
273
will be set to either `int`, `int64`, `decimal`, or `float`, in that order of preference.
271
274
272
-
If a value is missing in any row, by default the CSV type provider will infer a nullable (for `int` and `int64`) or an optional
273
-
(for `bool`, `DateTime` and `Guid`). When a `decimal` would be inferred but there are missing values, we will infer a
275
+
On .NET 6 and later, when you set `PreferDateOnly = true`, columns whose values are all date-only strings (without a time component, e.g. `2023-01-15`)
276
+
are inferred as `DateOnly`. Time-only strings are inferred as `TimeOnly`. If a column mixes `DateOnly` and `DateTime` values, it is unified to `DateTime`.
277
+
By default (`PreferDateOnly = false`), all date values are inferred as `DateTime` for backward compatibility.
278
+
279
+
If a value is missing in any row, by default the CSV type provider will infer a nullable (for `int`, `int64`, and `DateOnly`) or an optional
280
+
(for `bool`, `DateTime`, `DateTimeOffset`, and `Guid`). When a `decimal` would be inferred but there are missing values, we will infer a
274
281
`float` instead, and use `Double.NaN` to represent those missing values. The `string` type is already inherently nullable,
275
282
so by default, we won't generate a `string option`. If you prefer to use optionals in all cases, you can set the static parameter
276
283
`PreferOptionals` to `true`. In that case, you'll never get an empty string or a `Double.NaN` and will always get a `None` instead.
@@ -303,6 +310,12 @@ specify the units of measure. This will override both `AssumeMissingValues` and
303
310
*`guid`
304
311
*`guid?`
305
312
*`guid option`
313
+
*`dateonly` (.NET 6+ only)
314
+
*`dateonly?` (.NET 6+ only)
315
+
*`dateonly option` (.NET 6+ only)
316
+
*`timeonly` (.NET 6+ only)
317
+
*`timeonly?` (.NET 6+ only)
318
+
*`timeonly option` (.NET 6+ only)
306
319
*`string`
307
320
*`string option`.
308
321
@@ -373,6 +386,23 @@ for row in titanic2.Rows |> Seq.truncate 10 do
373
386
374
387
You can even mix and match the two syntaxes like this `Schema="int64,DidSurvive,PClass->Passenger Class=string"`
375
388
389
+
### DateOnly and TimeOnly (on .NET 6+)
390
+
391
+
On .NET 6 and later, when you set `PreferDateOnly = true`, date-only strings are inferred as `DateOnly`
392
+
and time-only strings as `TimeOnly`. For example, a column like `EventDate` containing values such as
393
+
`2023-06-01` will be given the type `DateOnly`. By default (`PreferDateOnly = false`), dates are
394
+
inferred as `DateTime` for backward compatibility.
395
+
396
+
You can also explicitly request a `DateOnly` or `TimeOnly` column using schema annotations:
397
+
398
+
[lang=text]
399
+
EventDate (dateonly),Duration (timeonly?)
400
+
2023-06-01,08:30:00
401
+
2023-06-02,
402
+
403
+
In the example above, `EventDate` is explicitly annotated as `dateonly` and `Duration` is explicitly
404
+
annotated as `timeonly?` (a nullable `TimeOnly`).
405
+
376
406
## Transforming CSV files
377
407
378
408
In addition to reading, `CsvProvider` also has support for transforming the row collection of CSV files. The operations
"""<summary>Typed representation of a CSV file.</summary>
@@ -246,7 +257,8 @@ type public CsvProvider(cfg: TypeProviderConfig) as this =
246
257
<param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless <c>charset</c> is specified in the <c>Content-Type</c> response header.</param>
247
258
<param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
248
259
<param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
249
-
(e.g. 'MyCompany.MyAssembly, resource_name.csv'). This is useful when exposing types generated by the type provider.</param>"""
260
+
(e.g. 'MyCompany.MyAssembly, resource_name.csv'). This is useful when exposing types generated by the type provider.</param>
261
+
<param name='PreferDateOnly'>When true on .NET 6+, date-only strings are inferred as DateOnly and time-only strings as TimeOnly. Defaults to false for backward compatibility.</param>"""
250
262
251
263
do csvProvTy.AddXmlDoc helpText
252
264
do csvProvTy.DefineStaticParameters(parameters, buildTypes)
0 commit comments