ratansunpy.time package¶
Submodules¶
ratansunpy.time.time module¶
- class ratansunpy.time.time.TimeFormats(common_formats: List[str] = <factory>, examples: List[str] = <factory>)[source]¶
Bases:
object
- common_formats: List[str]¶
- examples: List[str]¶
- ratansunpy.time.time.check_equal_time(t1: Time, t2: Time) bool [source]¶
Check if two time objects are equal within a nanosecond precision.
- Parameters:
t1 (Time) – The first time object.
t2 (Time) – The second time object.
- Returns:
True if the time difference is less than a nanosecond, False otherwise.
- Return type:
bool
Example:
>>> from astropy.time import Time >>> t1 = Time('2020-01-01T00:00:00.000000000') >>> t2 = Time('2020-01-01T00:00:00.000000001') >>> check_equal_time(t1, t2) # Returns: True
- ratansunpy.time.time.create_time(time_string: Any, format: str | None = None, **kwargs) Time [source]¶
- ratansunpy.time.time.create_time(time_string: Timestamp, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: Series, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: DatetimeIndex, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: datetime, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: date, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: datetime64, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: ndarray, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: Time, **kwargs) Time
- ratansunpy.time.time.create_time(time_list: list, format: str | None = None, **kwargs) Time
- ratansunpy.time.time.create_time(time_string: str, **kwargs) Time
Create an Astropy Time object from various time representations.
- Parameters:
time_string (Any) – The time data, which can be of various types.
format (Optional[str]) – The time format string, if known.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> create_time('2020-01-01T00:00:00.000000') # Returns: <Time object: scale='utc' format='isot' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.create_time_astropy(time_string: Time, **kwargs) Time [source]¶
Return the provided Astropy Time object.
- Parameters:
time_string (Time) – An Astropy Time object.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
The provided Astropy Time object.
- Return type:
Time
Example:
>>> t = Time('2020-01-01T00:00:00') >>> create_time(t) <Time object: scale='utc' format='isot' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.create_time_date(time_string: date, **kwargs) Time [source]¶
Convert a date object to an Astropy Time object.
- Parameters:
time_string (date) – A date object.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> d = date(2020, 1, 1) >>> create_time(d) <Time object: scale='utc' format='iso' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.create_time_datetime(time_string: datetime, **kwargs) Time [source]¶
Convert a datetime object to an Astropy Time object.
- Parameters:
time_string (datetime) – A datetime object.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> dt = datetime(2020, 1, 1) >>> create_time(dt) <Time object: scale='utc' format='datetime' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.create_time_list(time_list: list, format: str | None = None, **kwargs) Time [source]¶
Convert a list of time strings to an Astropy Time object.
- Parameters:
time_list (list) – A list of time strings.
format (Optional[str]) – The time format string, if known.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> time_list = ['2020-01-01T00:00:00'] >>> create_time(time_list) <Time object: scale='utc' format='isot' value=[2020-01-01T00:00:00.000]>
- ratansunpy.time.time.create_time_npdatetime64(time_string: datetime64, **kwargs) Time [source]¶
Convert a numpy datetime64 object to an Astropy Time object.
- Parameters:
time_string (np.datetime64) – A numpy datetime64 object.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> dt64 = np.datetime64('2020-01-01T00:00:00') >>> create_time(dt64) <Time object: scale='utc' format='iso' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.create_time_npndarray(time_string: ndarray, **kwargs) Time [source]¶
Convert a numpy ndarray of datetime64 objects to an Astropy Time object.
- Parameters:
time_string (np.ndarray) – A numpy ndarray of datetime64 objects.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> dt64_array = np.array(['2020-01-01T00:00:00'], dtype='datetime64') >>> create_time(dt64_array) <Time object: scale='utc' format='iso' value=[2020-01-01T00:00:00.000]>
- ratansunpy.time.time.create_time_pandasDatetimeIndex(time_string: DatetimeIndex, **kwargs) Time [source]¶
Convert a pandas DatetimeIndex to an Astropy Time object.
- Parameters:
time_string (pd.DatetimeIndex) – A pandas DatetimeIndex object.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> dt_index = pd.DatetimeIndex(['2020-01-01T00:00:00']) >>> create_time(dt_index) <Time object: scale='utc' format='datetime64' value=[2020-01-01T00:00:00.000]>
- ratansunpy.time.time.create_time_pandasSeries(time_string: Series, **kwargs) Time [source]¶
Convert a pandas Series of timestamps to an Astropy Time object.
- Parameters:
time_string (pd.Series) – A pandas Series of timestamp objects.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> ts_series = pd.Series([pd.Timestamp('2020-01-01T00:00:00')]) >>> create_time(ts_series) <Time object: scale='utc' format='datetime64' value=[2020-01-01T00:00:00.000]>
- ratansunpy.time.time.create_time_pandasTimestamp(time_string: Timestamp, **kwargs) Time [source]¶
Convert a pandas Timestamp to an Astropy Time object.
- Parameters:
time_string (pd.Timestamp) – A pandas Timestamp object.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> ts = pd.Timestamp('2020-01-01T00:00:00') >>> create_time(ts) <Time object: scale='utc' format='datetime64' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.create_time_str(time_string: str, **kwargs) Time [source]¶
Convert a time string to an Astropy Time object using common time formats.
- Parameters:
time_string (str) – A time string.
kwargs – Additional keyword arguments passed to
astropy.time.Time
.
- Returns:
An Astropy Time object.
- Return type:
Time
Example:
>>> create_time('2020-01-01T00:00:00') <Time object: scale='utc' format='isot' value=2020-01-01T00:00:00.000>
- ratansunpy.time.time.get_time_format(time_string: str) str | None [source]¶
Determine the time format of a given time string.
- Parameters:
time_string (str) – The time string to check.
- Returns:
The format string if a matching format is found, otherwise None.
- Return type:
Optional[str]
- Example:
>>> get_time_format('2020-01-01T00:00:00.000000') Returns: '%Y-%m-%dT%H:%M:%S.%f'
- ratansunpy.time.time.regex_time(time_string: str, format: str) str | None [source]¶
Match a time string against a format using regular expressions.
- Parameters:
time_string (str) – The time string to match.
format (str) – The time format string.
- Returns:
The time string if a match is found, otherwise None.
- Return type:
Optional[str]
Example:
>>> regex_time('2020-01-01T00:00:00.000000', '%Y-%m-%dT%H:%M:%S.%f') # Returns: '2020-01-01T00:00:00.000000'
ratansunpy.time.timerange module¶
- class ratansunpy.time.timerange.TimeRange(a: TimeRange | List[str | Time] | Tuple[str | Time, str | Time] | str | Time, b: str | Time | timedelta | TimeDelta | None = None, format: str | None = None)[source]¶
Bases:
object
A class to represent a time range, providing various utilities for time manipulation.
Attributes
start (Time): The start time of the range.
end (Time): The end time of the range.
delta (TimeDelta): The duration of the time range.
days (float): The duration of the time range in days.
hours (float): The duration of the time range in hours.
minutes (float): The duration of the time range in minutes.
seconds (float): The duration of the time range in seconds.
Methods
__init__(a, b=None, format=None): Initializes a TimeRange object.
__eq__(other): Checks if two TimeRange objects are equal.
__ne__(other): Checks if two TimeRange objects are not equal.
__contains__(time): Checks if a given time is within the time range.
__repr__(): Returns a string representation of the TimeRange object.
__str__(): Returns a simple string representation of the start and end times.
have_intersection(other): Checks if the time range intersects with another TimeRange.
get_dates(filter=None): Returns a list of dates within the time range, optionally excluding certain dates.
moving_window(window_size, window_period): Returns a list of TimeRange objects representing moving windows within the original time range.
equal_split(n_splits=2): Splits the time range into equal subranges.
shift_forward(delta=None): Shifts the time range forward by a specified delta or its own duration.
shift_backward(delta=None): Shifts the time range backward by a specified delta or its own duration.
extend_range(start_delta, end_delta): Extends the start and end times of the time range by the specified deltas.
- property days¶
- property delta¶
- property end¶
- equal_split(n_splits: int = 2) List[TimeRange] [source]¶
Split the time range into equal subranges.
- Parameters:
n_splits (int) – The number of subranges to divide the time range into. Must be greater than or equal to 1.
- Raises:
ValueError – If n_splits is less than or equal to 0.
- Returns:
A list of TimeRange objects representing the equal subranges.
- Return type:
list of TimeRange
- extend_range(start_delta: TimeDelta, end_delta: TimeDelta) self [source]¶
Extend the time range by modifying the start and end times.
- Parameters:
start_delta (TimeDelta) – The amount of time to add to the start of the range.
end_delta (TimeDelta) – The amount of time to add to the end of the range.
- Returns:
The instance with the extended time range.
- Return type:
self
- get_dates(filter: List[datetime] | None = None) List[datetime] [source]¶
Generate a list of dates within the time range defined by the instance.
- Parameters:
filter (list of datetime-like objects or None) – A list of dates to be excluded from the result. If provided, only dates not in this list will be included.
- Returns:
A list of dates from start to end of the time range, with optional exclusion.
- Return type:
list of datetime-like objects
- have_intersection(other: TimeRange) bool [source]¶
Checks if the time range intersects with another TimeRange.
- Parameters:
other (TimeRange) – Another TimeRange object to compare.
- Returns:
True if the ranges intersect, False otherwise.
- Return type:
bool``
- property hours¶
- property minutes¶
- moving_window(window_size: TimeDelta | int, window_period: TimeDelta | int) List[TimeRange] [source]¶
Generate a list of time ranges using a moving window approach.
- Parameters:
window_size (TimeDelta or int) – The duration of each time window.
window_period (TimeDelta or int) – The period to shift the window after each iteration.
- Returns:
A list of TimeRange objects representing the moving windows.
- Return type:
list of TimeRange
- property seconds¶
- shift_backward(delta: TimeDelta | None = None) self [source]¶
Shift the entire time range backward by a specified duration.
- Parameters:
delta (TimeDelta or None) – The duration by which to shift the time range. If not provided, the entire duration of the time range is used.
- Returns:
The instance with the updated time range.
- Return type:
self
- shift_forward(delta: TimeDelta | None = None) self [source]¶
Shift the entire time range forward by a specified duration.
- Parameters:
delta (TimeDelta or None) – The duration by which to shift the time range. If not provided, the entire duration of the time range is used.
- Returns:
The instance with the updated time range.
- Return type:
self
- property start¶