Is it possible to use f-strings with Concat(), Value() and F() - django

I have a varchar column whose values I would like to update by concatenating a prefix to a padded integer. Here is what I have tried so far:
Item.objects.update(
field=Concat(
Value('prefix'), Value(f"{F('id'):>011d}")
)
)
Which gives me TypeError: unsupported format string passed to F.__format__
I need help on how I can achieve this if possible.

Considering the fact that my use case of the f-string was padding, the LPAD and CAST database functions came in handy (I definitely need to study SQL). Here is the update query:
Item.objects.update(
field=Concat(
Value('prefix'), LPad(Cast('id', output_field=CharField()), 11, Value('0'))
)
)

Related

'LOOKUPVALUE' does not support comparing values of type Number with values of type True/False

I'm quite new in using DAX in PowerBi and I need to perform the following dax function:
Latest Detailed =
LOOKUPVALUE (
EXAM_REPORT[ACTUAL_EXAM_DATE],
EXAM_REPORT[CES EXAM.EXAM_TYPE_SR_KEY],
EXAM_REPORT[CES EXAM.EXAM_TYPE_SR_KEY] = 2,
EXAM_REPORT[CES EXAM.EXAM_SIGNOFF_DATE],
ISBLANK ( EXAM_REPORT[CES EXAM.EXAM_SIGNOFF_DATE] ) = FALSE,
EXAM_REPORT[ASSET_GUID],
[ASSET_GUID] = [ASSET_GUID]
)
Unfortunately I keep getting this error:
Function 'LOOKUPVALUE' does not support comparing values of type Number with values of type True/False. Consider using the VALUE or FORMAT function to convert one of the values.
I’ve tried converting everything to strings and also tried VALUE as well, but nothing changes.
Could you please help me? How can I do in a way that all the values share the same datatype?
LOOKUPVALUE has a different signature than what you are using:
LOOKUPVALUE(
<result_columnName>,
<search_columnName>,
<search_value>
[, <search2_columnName>, <search2_value>]…
[, <alternateResult>]
)
Your Code:
LOOKUPVALUE(
<result_columnName>,
<search_columnName>,
<search_columnName> = <search_value>
)
And FALSE must be FALSE(), but the condition is inappropriate anyway.

How can i convert string to int and then sort view by int value?

I want sort view by a value that is string. but before that, i want convert string to int then sort by that.
main = models.Main.objects.all().order_by('fore_key__n')
In this code fore_key__n is string value like '20'
Annotations and DB functions can probably do this. Cast the string value to an int and then use it to order the queryset. I haven't ever had cause to try this, so treat the following as a suggestion:
main = models.Main.objects.annotate(
fkn_int_cast=Cast('fore_key__n', output_field=IntegerField()),
).order_by('fkn_int_cast')
It will throw a django.db.utils.DataError should the data in the field not be capable of conversion. Therefore, it's probably necessary to apply a regex filter as well
main = models.Main.objects.filter(
fore_key_n__regex='^[0-9]+$'
).annotate(
fkn_int_cast=Cast('fore_key__n', output_field=IntegerField()),
).order_by('fkn_int_cast')
There are other DB functions you might use, for example, to replace the commas in '1,234,456' with null strings so it becomes Cast'able

Can I directly pass string parameter to Quicksight function as argument?

I made a parameter with a custom list of options 'MM', 'YYYY', and 'Q'. When a user selects one, I planned my calculated field to use it as an argument for the extract() function, like this:
extract(${period}, date)
I tried to omit the quotes, include them, but nothing works, saying "At least one of the arguments in this function does not have correct type."
Is what I want to make possible?
From the little testing I've done it looks like extract requires a string literal as its first argument. This could be a bug and may be worth bringing to Amazon's attention.
As a workaround, you could solve this by using ifelse
ifelse(
${period} = 'MM', extract('MM', {Date}),
${period} = 'YYYY', extract('YYYY', {Date}),
extract('Q', {Date})
)
This is actually kind of nice because it gives you the opportunity to make the filter control more readable (e.g. Month, Year, Quarter) then do
ifelse(
${period} = 'Month', extract('MM', {Date}),
${period} = 'Year', extract('YYYY', {Date}),
extract('Q', {Date})
)
This works for your example because your grouping options are well defined, however, it wouldn't work for a dynamic, less understood set of controls.

Randomly set one-third of na's in a column to one value and the rest to another value

I'm trying to impute missing values in a dataframe df. I have a column A with 300 NaN's. I want to randomly set 2/3rd of it to value1 and the rest to value2.
Please help.
EDIT: I'm actually trying to this on dask, which does not support item assignment. This is what I have currently. Initially, I thought I'll try to convert all NA's to value1
da.where(df.A.isnull() == True, 'value1', df.A)
I got the following error:
ValueError: need more than 0 values to unpack
As the comment suggests, you can solve this with Series.where.
The following will work, but I cannot promise how efficient this is. (I suspect it may be better to produce a whole column of replacements at once with numpy.choice.)
df['A'] = d['A'].where(~d['A'].isnull(),
lambda df: df.map(
lambda x: random.choice(['value1', 'value1', x])))
explanation: if the value is not null (NaN), certainly keep the original. Where it is null, replace with the corresonding values of the dataframe produced by the first lambda. This maps values of the dataframe (chunks) to randomly choose the original value for 1/3 and 'value1' for others.
Note that, depending on your data, this likely has changed the data type of the column.

How to Select Date Range using RegEx

I have date strings that looks like so:
20120817110329
Which, as you can see, is formatted: YYYYMMDDHHMMSS
How would I select (using RegEx) dates that are between 7/15 and 8/20? Or what about 8/1 to 8/15?
I have this working if I want to select a range that doesn't involve more than one place, but it is very limited:
^2012081[0-7] //selects 8/10 to 8/17
Update
Never forget the obvious (as pointed out by Wiseguy below), one can simply look for a range between 201207150000 and 201208209999.
Since you're just querying a database field that contains these values, you could simply check for a value between 201207150000 and 201208209999.
If you still want the regex, it ain't pretty, but this does it:
^20120(7(1[5-9]|2\d|3[01])|8([0-1]\d|20))\d{4}$
reFiddle example
You basically have to account for each possible range by hand.
^20120
(
7
(
1[5-9]
|2\d
|3[01]
)
|
8
(
[0-1]\d
|20
)
)
\d{4}$
I think this should work:
^2012(07(1[5-9]|[2-3][0-9])|08([0-1][0-9]|20))
Although the other answers are pretty the same...
You can check this for more info.