Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
finam
Manage
Activity
Members
Labels
Plan
Issues
32
Issue boards
Milestones
Code
Merge requests
4
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FINAM
finam
Commits
abf3f983
Commit
abf3f983
authored
2 years ago
by
Martin Lange
Browse files
Options
Downloads
Patches
Plain Diff
add optional forcing of copy in to_xarray
parent
3389893a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!217
Ensure data is a copy in outputs
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/finam/data/tools.py
+18
-3
18 additions, 3 deletions
src/finam/data/tools.py
with
18 additions
and
3 deletions
src/finam/data/tools.py
+
18
−
3
View file @
abf3f983
...
@@ -64,7 +64,7 @@ def _gen_dims(ndim, info):
...
@@ -64,7 +64,7 @@ def _gen_dims(ndim, info):
return
dims
return
dims
def
to_xarray
(
data
,
name
,
info
,
time
=
None
,
no_time_check
=
False
):
def
to_xarray
(
data
,
name
,
info
,
time
=
None
,
force_copy
=
False
,
no_time_check
=
False
):
"""
"""
Convert data to a xarray.DataArray.
Convert data to a xarray.DataArray.
...
@@ -78,7 +78,11 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
...
@@ -78,7 +78,11 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
Info associated with the data.
Info associated with the data.
time : :class:`datetime <datetime.datetime>` or None, optional
time : :class:`datetime <datetime.datetime>` or None, optional
Timestamp for the data, by default None
Timestamp for the data, by default None
no_time_check : bool
force_copy : bool, optional
Forces the result to be a copy of the passed data. Default `False`.
If not used, the result is a view of the data if no units conversion needs to be done.
no_time_check : bool, optional
Skips the time check for xarray input data. Used internally in adapter outputs.
Skips the time check for xarray input data. Used internally in adapter outputs.
Returns
Returns
...
@@ -101,6 +105,12 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
...
@@ -101,6 +105,12 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
ignore_time
=
no_time_check
,
ignore_time
=
no_time_check
,
check_units_equivalent
=
False
,
check_units_equivalent
=
False
,
)
)
if
equivalent_units
(
info
.
units
,
data
):
if
force_copy
:
return
data
.
copy
()
return
data
return
to_units
(
data
,
info
.
units
)
return
to_units
(
data
,
info
.
units
)
units
=
UNITS
.
Unit
(
info
.
units
)
units
=
UNITS
.
Unit
(
info
.
units
)
...
@@ -110,9 +120,14 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
...
@@ -110,9 +120,14 @@ def to_xarray(data, name, info, time=None, no_time_check=False):
f
"
Given data has incompatible units.
"
f
"
Given data has incompatible units.
"
f
"
Got
{
data
.
units
}
, expected
{
units
}
.
"
f
"
Got
{
data
.
units
}
, expected
{
units
}
.
"
)
)
data
=
np
.
asarray
(
data
.
m_as
(
units
))
if
force_copy
and
equivalent_units
(
data
.
units
,
units
):
data
=
np
.
asarray
(
data
.
m_as
(
units
)).
copy
()
else
:
data
=
np
.
asarray
(
data
.
m_as
(
units
))
else
:
else
:
data
=
np
.
asarray
(
data
)
data
=
np
.
asarray
(
data
)
if
force_copy
:
data
=
data
.
copy
()
time_entries
=
(
time_entries
=
(
len
(
time
)
if
time
is
not
None
and
not
isinstance
(
time
,
datetime
.
datetime
)
else
1
len
(
time
)
if
time
is
not
None
and
not
isinstance
(
time
,
datetime
.
datetime
)
else
1
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment