Skip to content

Commit

Permalink
#811: add weekday name column builder
Browse files Browse the repository at this point in the history
  • Loading branch information
aschonfeld committed Oct 2, 2023
1 parent bf8c54a commit e9ac649
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions dtale/column_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def __init__(self, name, cfg):
def build_column(self, data):
col = self.cfg["col"]
if "property" in self.cfg:
if self.cfg["property"] == "weekday_name":
return data[col].dt.day_name()
return getattr(data[col].dt, self.cfg["property"])
conversion_key = self.cfg["conversion"]
[freq, how] = conversion_key.split("_")
Expand All @@ -227,6 +229,10 @@ def build_column(self, data):

def build_code(self):
if "property" in self.cfg:
if self.cfg["property"] == "weekday_name":
return "df.loc[:, '{name}'] = df['{col}'].dt.day_name()".format(
name=self.name, **self.cfg
)
return "df.loc[:, '{name}'] = df['{col}'].dt.{property}".format(
name=self.name, **self.cfg
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ describe('CreateColumn buildCode tests', () => {
conversion: DatetimeConversionType.MONTH_END,
});
expect(code).toBe("df['col1'].dt.hour");
code = buildDatetimeCode({
col: 'col1',
operation: DatetimeOperation.PROPERTY,
property: DatetimePropertyType.WEEKDAY_NAME,
conversion: DatetimeConversionType.MONTH_END,
});
expect(code).toBe("df['col1'].dt.day_name()");
});

it('Bins buildCode test', () => {
Expand Down
1 change: 1 addition & 0 deletions frontend/static/popups/create/CreateColumnState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export enum DatetimePropertyType {
TIME = 'time',
DATE = 'date',
WEEKDAY = 'weekday',
WEEKDAY_NAME = 'weekday_name',
MONTH = 'month',
QUARTER = 'quarter',
YEAR = 'year',
Expand Down
3 changes: 3 additions & 0 deletions frontend/static/popups/create/CreateDatetime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const buildCode = (cfg: DatetimeConfig): CreateColumnCodeSnippet => {
if (!property) {
return undefined;
}
if (property === DatetimePropertyType.WEEKDAY_NAME) {
return `df['${col}'].dt.day_name()`;
}
code = `df['${col}'].dt.${property}`;
} else {
if (!conversion) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/static/translations/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
"Time": "时间",
"Date": "日期",
"Weekday": "工作日",
"Weekday Name": "工作日 名字",
"Month": "",
"Quarter": "季度",
"Year": "",
Expand Down Expand Up @@ -651,7 +652,7 @@
"Action": "Action",
"Dataset": "Dataset",
"ID": "ID",
"Name": "Name",
"Name": "名字",
"Cols": "Cols",
"Rows": "Rows",
"Index(es)*": "Index(es)*",
Expand Down
1 change: 1 addition & 0 deletions frontend/static/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
"Time": "Time",
"Date": "Date",
"Weekday": "Weekday",
"Weekday Name": "Weekday Name",
"Month": "Month",
"Quarter": "Quarter",
"Year": "Year",
Expand Down
1 change: 1 addition & 0 deletions frontend/static/translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
"Time": "Tempo",
"Date": "Data",
"Weekday": "Dia da semana",
"Weekday Name": "Weekday Name",
"Month": "Mês",
"Quarter": "Trimestre",
"Year": "Ano",
Expand Down
11 changes: 10 additions & 1 deletion tests/dtale/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,16 @@ def test_build_column():
else "int64"
)

for p in ["minute", "hour", "time", "date", "month", "quarter", "year"]:
for p in [
"minute",
"hour",
"time",
"date",
"weekday_name",
"month",
"quarter",
"year",
]:
c.get(
"/dtale/build-column/{}".format(c.port),
query_string=dict(
Expand Down

0 comments on commit e9ac649

Please sign in to comment.