Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data time accessor with persiantools #8

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
39 changes: 39 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Upload Python Package

on:
release:
types: [published]

permissions:
contents: read

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
4 changes: 2 additions & 2 deletions jalali_pandas/df_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
from typing import List, Union
import pandas as pd
import jdatetime
from persiantools.jdatetime import JalaliDateTime

# pylint: disable=unused-import
# from .serie_handler import JalaliSerieAccessor
Expand Down Expand Up @@ -38,7 +38,7 @@ def __validate(self):
pandas_obj (pd.DataFrame): [description]
"""
for col in self.columns:
if isinstance(self._obj[col].iloc[0], jdatetime.date):
if isinstance(self._obj[col].iloc[0], JalaliDateTime):
print(f'Column "{col}" will be the refrence.')
self.jdate = col
return
Expand Down
14 changes: 7 additions & 7 deletions jalali_pandas/serie_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
handle jalaali dates in pandas series
"""
import jdatetime
import pandas as pd
from persiantools.jdatetime import JalaliDateTime


@pd.api.extensions.register_series_accessor("jalali")
Expand All @@ -28,7 +28,7 @@ def __validate(self):
TypeError: [description]
"""

if not all(isinstance(x, (str, jdatetime.date)) for x in self._obj):
if not all(isinstance(x, (str, JalaliDateTime)) for x in self._obj):
raise TypeError("pandas series must be jdatetime or string of jdate")

def to_jalali(self) -> pd.Series:
Expand All @@ -37,7 +37,7 @@ def to_jalali(self) -> pd.Series:
Returns:
pd.Series: pd.Series of jalali datetime.
"""
return self._obj.apply(lambda x: jdatetime.datetime.fromgregorian(date=x))
return self._obj.apply(lambda x: JalaliDateTime.to_jalali(x))

def to_gregorian(self) -> pd.Series:
"""convert jalali datetime to python default datetime.
Expand All @@ -46,7 +46,7 @@ def to_gregorian(self) -> pd.Series:
pd.Series: pd.Series of python datetime.
"""

return self._obj.apply(jdatetime.datetime.togregorian)
return self._obj.apply(JalaliDateTime.to_gregorian)

# pylint: disable=redefined-builtin
def parse_jalali(self, format: str = "%Y-%m-%d") -> pd.Series:
Expand All @@ -58,7 +58,7 @@ def parse_jalali(self, format: str = "%Y-%m-%d") -> pd.Series:
Returns:
pd.Series: pd.Series of jalali datetime.
"""
return self._obj.apply(lambda x: jdatetime.datetime.strptime(x, format))
return self._obj.apply(lambda x: JalaliDateTime.strptime(x, format))

@property
def year(self) -> pd.Series:
Expand Down Expand Up @@ -132,14 +132,14 @@ def weekday(self) -> pd.Series:
return self._obj.apply(lambda x: x.weekday())

@property
def weeknumber(self) -> pd.Series:
def week_of_year(self) -> pd.Series:
"""get Jalali day of year

Returns:
pd.Series: Jalali day of year
"""
self.__validate()
return self._obj.apply(lambda x: x.weeknumber())
return self._obj.apply(lambda x: x.week_of_year())

@property
def quarter(self):
Expand Down
Binary file modified requirements.txt
Binary file not shown.
16 changes: 7 additions & 9 deletions tests/df_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import pandas as pd
import pytest
from jalali_pandas import ( # pylint: disable=W0611
JalaliSerieAccessor,
JalaliDataframeAccessor,
JalaliSerieAccessor, # noqa: F401
JalaliDataframeAccessor, # noqa: F401
)


Expand All @@ -30,27 +30,25 @@ def df(self) -> pd.DataFrame:
def test_jalali_groupby(self):
"""Test jalali property like year, month, weeknumber"""
df = self.df
mean = df.jalali.groupby("year").mean()
mean = df.jalali.groupby("year").any()
assert (mean.index == [1397, 1398]).all(), "Year grouping is wrong"
mean = df.jalali.groupby("month").mean()
mean = df.jalali.groupby("month").any()
assert not set(mean.index).difference(
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
), "computaion is wrong"

def test_jalali_groupby_shorts(self):
"""Test jalali property like ymd, ym, yq, md"""
df = self.df
mean = df.jalali.groupby("ymd").mean()
mean = df.jalali.groupby("ymd").any()
assert mean.index.names == [
"__year",
"__month",
"__day",
], "ymd grouping is wrong"
mean = df.jalali.groupby("ym").mean()
mean = df.jalali.groupby("ym").any()
assert mean.index.names == ["__year", "__month"], "ym grouping is wrong"
mean = df.jalali.groupby("yq").mean()
assert mean.index.names == ["__year", "__quarter"], "yq grouping is wrong"
mean = df.jalali.groupby("md").mean()
mean = df.jalali.groupby("md").any()
assert mean.index.names == ["__month", "__day"], "md grouping is wrong"

def test_check_wrong_groupby(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/series_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test Series class
"""
import jdatetime
from persiantools.jdatetime import JalaliDateTime
import pandas as pd
import pytest
from jalali_pandas import ( # pylint: disable=W0611
Expand All @@ -26,7 +26,7 @@ def df(self) -> pd.DataFrame:
def test_jalali_convertor(self):
"""Test jalali convertor from gregorian to jalali"""
df = self.df
assert df["jdate"].iloc[0] == jdatetime.datetime(year=1397, month=10, day=11)
assert df["jdate"].iloc[0] == JalaliDateTime(year=1397, month=10, day=11)
assert df["date"].iloc[0] == pd.Timestamp("2019-01-01")

def test_gregorian_convertor(self):
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_jalali_property(self):
assert df["jdate"].jalali.minute[0] == 0, "minute is not 0"
assert df["jdate"].jalali.second[0] == 0, "second is not 0"
assert df["jdate"].jalali.weekday[0] == 3, "weekday is not 4"
assert df["jdate"].jalali.weeknumber[0] == 42, "weeknumber is not 42"
assert df["jdate"].jalali.week_of_year[0] == 42, "weeknumber is not 42"
assert df["jdate"].jalali.quarter[0] == 4, "quarter is not 4"


Expand Down